IIS URL Rewrite - wcf

I have the following rewrite rule:
<rewrite>
<rules>
<rule name="FrontController" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="wcf/api.svc/auth/home" />
</rule>
</rules>
</rewrite>
This basically rewrites all non-file urls to web service api calls that returns index.html in an SPA backed by WCF.
The above rewrite ends up including all the query string parameters that were included with the original URL. What I need to do is also include the original URL, such as, 'wcf/api.svc/auth/products', as a query string parameter in the rewritten URL, such as 'https://domain.com/wcf/api.svc/auth/products?enc=lkjewro8xlkz' being transformed into 'https://domain.com/wcf/api.svc/auth/home?enc=lkjewro8xlkz&orig=wcf/api.svc/auth/products'.
Is this possible, and if so, what changes would I need to make to achieve it? I would like for my WCF application to know about the original URL so that it can configure the SPA to initialize to a particular view on load.
Thanks

It's quite possible.
You need to add the URL Encoded value of the {REQUEST_URI} to the Rewrite URL.
<rewrite>
<rules>
<rule name="FrontController" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="wcf/api.svc/auth/home?orig={UrlEncode:{REQUEST_URI}}" />
</rule>
</rules>
</rewrite>
With this rule, in your WCF endpoint orig parameter would be:
/wcf/api.svc/auth/products?enc=lkjewro8xlkz
If you don't want the query string part (?enc=lkjewro8xlkz), you'll need an extra condition to match the URI without query string.
<rewrite>
<rules>
<rule name="FrontController" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<!-- match any character up to a question mark -->
<add input="{REQUEST_URI}" pattern="^[^\?]+" />
</conditions>
<!-- {C:0} means the first match in conditions -->
<action type="Rewrite" url="wcf/api.svc/auth/home?orig={UrlEncode:{C:0}}" />
</rule>
</rules>
</rewrite>
Now, orig will be /wcf/api.svc/auth/products in the WCF endpoint.
Hope it helps.

Related

URL Rewriting in IIS with Express

I want to rewrite all images to a different folder. I'm using IIS and have configured a rule in the web.config to redirect all requests to a node.js file as follows:
<rewrite>
<rules>
<rule name="img">
<match url="\/(.*).img" />
<action type="Rewrite" url="/handlers/img.js" />
</rule>
</rules>
</rewrite>
All requests are now being sent to the img.js file, where based on a condition, I want to redirect to another image file. But IIS now sends that file to the img.js and it ends up as a loop. Is there any way out of this loop?
You could try the below thing to resolve the issue:
set the condition to do not match the pattern:
<conditions>
<add input="{REQUEST_URI}" pattern="\/(.*).img.js" negate="true" />
</conditions>
or set <rule name="img" stopProcessing="true">
<rule name="img" stopProcessing="true">
<match url="\/(.*).img" />
<conditions>
<add input="{REQUEST_URI}" pattern="\/(.*).img.js" negate="true" />
</conditions>
<serverVariables />
<action type="Rewrite" url="/handlers/img.js" logRewrittenUrl="true" />
</rule>

Replace specific query string value with IIS redirect maps

I'm trying to create a redirect rule for a .NET Core project that would replace a query string value so to help deploy a migration on some identifiers. For example the URL:
http://www.somesite.com/page.html?somevar=abc&id=1234&othervar=cde
should redirect to
http://www.somesite.com/page.html?somevar=abc&id=b64540a6-b12d-443f-973f-673e4451ccbe&othervar=cde
On the rewrite file I've created a rewrite map as such:
<rewriteMaps>
<rewriteMap name="newIdentifierMap">
<add key="1234" value="b64540a6-b12d-443f-973f-673e4451ccbe" />
</rewriteMap>
</rewriteMaps>
But now I'm having some trouble creating the rule to use this map. This is what I was doing...
<rule>
<match url=".*" />
<conditions>
<add input="{newIdentifierMap:REQUEST_URI}" pattern="(.*)id=([0-9]+)(.*)" />
</conditions>
<action type="Redirect" url="" RedirectType="Permanent" />
</rule>
Now I'm block without knowing how exactly to replace the values in the map.
Thanks for any help!
Solution
After checking the answer provided by #abraham-qian I was able to solve the issue by applying the following rules:
<rule>
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)(\bid=([0-9]+))(.*)" />
</conditions>
<action type="Redirect" url="{R:0}?{C:1}id={newIdentifierMap:{C:3}}{C:4}" RedirectType="Permanent" appendQueryString="false" />
</rule>
<rewriteMaps>
<rewriteMap name="newIdentifierMap">
<add key="1234" value="b64540a6-b12d-443f-973f-673e4451ccbe" />
</rewriteMap>
</rewriteMaps>
This seems to be working fine with any number of variables before or after the id variable in the querystring.
In order to match the ID segment, we could use {Query_String} server variable.
Please refer to below code snippets,
<rewrite>
<rules>
<rule name="MyRules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)(\bid=([0-9]+))(.*)" />
</conditions>
<!--{C:3} is the value of id, it is referred by the above condition.-->
<action type="Redirect" url="Https://vabqia969vm:448/{MyMap:{C:3}}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MyMapName" defaultValue="">
<add key="1234" value="HtmlPage1.html"></add>
</rewriteMap>
</rewriteMaps>
</rewrite>
However, there is an issue that we should uncheck the option Append Query String, or will cause an endless loop since the appended query string meets the condition. We have to match each query field one by one. Like this,
(somevar=.*)&(\bid=([0-9]+))&(othervar=.*)
We had better modify the rules according to your situation.
Feel free to let me know if there is anything I can help with.

IIS rewrite rule Redirect Non-www to dynamic Domain Equivalent and always https

What I want is that all requests that are non-https or don't have www prepended are redirected to: "https://www." + domain name + possible query string parameters.
I have this rewrite rule (found here):
<rule name="non-www to www https" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>
However, when typing the following domains in the browser address bar no redirect takes place (and I get a security certificate error since I don't have a wildcard DNS SSL certificate):
https://example.com/
http://example.com/
But example.com (without protocol), redirects correctly to https://www.example.com/
Also notice in the above rule that I'm matching the hostname dynamically and not just on "example.com" since I want this rule to work for multiple domain names.
I then also checked this post, which has a neat rule:
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
I think this does exactly what I want, but how would I make the domain name in this example dynamic and preserve that in the redirect (like the first code sample does)? (the original poster has not logged in in the last 6 months so that's why I am asking here)
Furthermore I also checked this post, which also seems a good candidate:
<rule name="Redirect top domains with non-www to www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
<add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
<serverVariables>
<set name="Redirect" value="false" />
</serverVariables>
</rule>
<rule name="Force HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
But then http://example.com redirects to https://example.com and I still get the security exception.
First, I strongly recommend you to obtain a new SSL certificate that supports both example.com and www.example.com. That kind of certificates are actually pretty standard with most SSL providers, it does not have to be a wildcard certificate. Otherwise you will not be able to handle requests to https://example.com as it is now, and that's a problem I think.
Your top two rules should be like the ones below.
P.S. 301 redirects are cached for a while by the browsers. Google clear 301 redirect cache for your browser before testing the new rules.
<rule name="All HTTP to HTTPS+WWW" stopProcessing="true">
<match url=".*" />
<conditions trackAllCaptures="true">
<add input="{SERVER_PORT_SECURE}" pattern="0" />
<add input="{HTTP_HOST}" pattern="(?:localhost|stage\.|dev\.)" negate="true" />
<!-- here with this 3rd condition we capture the host name without "www." prefix into {C:1} variable to use in redirect action -->
<add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
</conditions>
<action type="Redirect" url="https://www.{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="All HTTPS With No WWW to HTTPS+WWW" stopProcessing="true">
<match url=".*" />
<conditions trackAllCaptures="false">
<add input="{SERVER_PORT_SECURE}" pattern="1" />
<add input="{HTTP_HOST}" pattern="(?:localhost|stage\.|dev\.)" negate="true" />
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>

URL Rewrite rule - subfolder to query string value

I'm trying to create a rule the will take the subfolder from the URL and convert that to a query string value for example:
If I navigated to this URL: http://www.example.com/myfolder
I would like that to read http://www.example.com/default.aspx?folder=myfolder
This is where I'm up to:
<rule name="Rewrite Language">
<match url="([a-z]{2})(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/default.aspx?code={R:2}" />
</rule>
but this doesn't return the full subfolder value. I'll be honest I've stole this from a similar issue from this site, and I must confess I really have no idea what it all means!
I might be approaching this in the wrong way, my issue is that I can't be sure what the subfolder will be as this is generated dynamically from a random 6 character alphanumeric value.
Any help would be very much appreciated.
David
The IIS Manager has a GUI / wizard interface for creating the rules which I usually find quicker and easier than entering the rule into the web.config file manually. Worth checking out: IIS Manager -> select your site / application -> URL Rewrite -> Add Rule(s).
I think the following rule will do the trick for you:
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="default.aspx?folder={R:1}" />
</rule>
Basically, the "match url" is a regular expression that is used to identify a part of the URL. In this case, it captures a group containing one or more characters (except for a /), with an optional / at the end of the URL. It will then rewrite the url to default.aspx?folder= followed by the value that was matched ({R:1} refers to the first captured group, which will contain the folder name).
This will work provided you only have a single subfolder name (not nested folders).
You could also add a second rule which works in the opposite direction, so browsing to http://www.example.com/default.aspx?folder=myfolder would result in the user seeing http://www.example.com/myfolder:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^default\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^folder=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>

Preserving URL when using SSL Redirect for multiple websites pointing to same folder

I have multiple websites pointing to a central folder (IIS 7.5)
company1.domain.com/wo pointing to D:\inetpub\wo
company2.domain.com/wo pointing to D:\inetpub\wo
company3.domain.com/wo pointing to D:\inetpub\wo
All the websites work for both HTTP and HTTPS (if typed manually). However, the sites have to connect via HTTPS. I want to setup automatic SSL redirect to but am having issues. I created URL Rewrite rule but since this is only one webconfig file the URL redirects to only one website (not maintaining the URL).
How do I setup SSL redirect so that the URLs are preserved and all websites point to the same folder?
Any assistance will be greatly appreciated.
Thanks
You should include the host header when checking if HTTPS is enabled and then redirect to the https URL for the appropriate domain.
Here's an example:
<rewrite>
<rules>
<clear />
<rule name="Force HTTPS - www.domain1.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" negate="true" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="\.domain1\.com$" />
</conditions>
<action type="Redirect" url="https://www.domain1.com{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Force HTTPS - www.domain2.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" negate="true" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="\.domain2\.com$" />
</conditions>
<action type="Redirect" url="https://www.domain2.com{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
<!-- add more rules for other domains if needed -->
</rule>
</rules>
</rewrite>
You can add as many rules for domain names as you want.
EDIT: Sorry, I misread your question. In that case it's even simpler:
<rewrite>
<rules>
<clear />
<rule name="Force HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" negate="true" pattern="^ON$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
No need to check for the host header, just include the host name in the redirect. You only have to make sure that you have SSL certificates for all domain names.