Setting up URL Rewrite rule for a specific domain - iis

For local dev testing, I need to catch all requests to www.somedomain.com/XXX (where X is the path), and send them on to localhost/somevdir/XXX.
I've added this to my HOSTS file (c:\windows\system32\drivers\etc):
127.0.0.1 www.mydomain.com
Then, in IIS8 (Windows 8), I've added a binding to my "Default Web Site" for the host www.mydomain.com. This works, I can now browse www.mydomain.com/test.html and see a test html page. My virtual dir is inside the Default web site. I then add a URL Rewrite URL to the web site for the last bit:
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url="^www.mydomain.com/(.*)" />
<action type="Rewrite" url="localhost/MyVDir/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
But - that doesn't work. I get a 404 so it looks the the match never happens. I've tried redirect and rewrite, and I've tried without the ^ in the regex and a few other regex tweaks. Can someone explain what I've done wrong?

I think the following should work:
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>
The match on any URL makes sure the conditions are checked, and the HTTP_HOST server variable seems the most reliable way of checking the requested hostname. You could remove the REQUEST_FILENAME input condition, but it works as quite a nice sanity check to make sure static files are always served.

The following is better for catching both www. and non-www. versions of the domain so that you don't have to write the domain twice, which could possibly cause errors with being twice as likely to write a typo. (The parenthesis with the ? character means optional in regex terms.)
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain\.com$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>

Related

IIS 10 URL Rewrite http traffic to https redirect not working

IIS 10 server behind an AWS application load balancer will not redirect traffic for domain without www when client requests http rather than https. The rule to redirect traffic when www is specified works fine, but 404 is returned if you try the same url without www.
So:
Enter "http://dname.com/blog" = 404
Enter "http://www.dname.com/blog" = redirect to "https://www.dname.com/blog"
<rule name="Force HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
<add input="{HTTP_HOST}" pattern="^dname\.com$" />
</conditions>
<action type="Rewrite" url="https://www.dname.com{REQUEST_URI}" />
</rule>
<rule name="Force WWW HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
<add input="{HTTP_HOST}" pattern="^www\.dname\.com$" />
</conditions>
<action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
</rule>
Nothing worked for me even after going through the answers provided on different forums.
After 2 days of banging my head in this here's what I found which solved the issue :
Bindings : Port 80 must be enabled (This can be added in bindings section in IIS).
SSL settings : Required SSL must be unchecked.
Add Rule :
<rewrite>
<rules>
<rule name="http to https redirection" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
appendQueryString="false" />
</rule>
</rules>
</rewrite>
Verify web config as it should reflect the rule added in IIS.
I don't know why the previously posted rules wouldn't work, but I was able to create a refined rule that is working:
<rule name="Force HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
<add input="{HTTP_HOST}" pattern="^(www\.)?dname\.com$" />
</conditions>
<action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
</rule>
The above rule combines the two rules instead of looking for the domain without the www and then with the www in a separate rule. The regex (www\.) tells the rule to look for "www." and the question mark tells it that it may or may not be there, so that includes the domain with and without the www.
There is a very very important step that should take care, before setup a redirect configure.
in web Sites project --> Actions(in the right) --> Bindings , the content will like below:
Binding Content
You take carefully the yellow color part, the yellow part is your original web IP address. This original IP address must exist in "Site Bindings", without the yellow part the URL redirect will not working anymore.
The following config is my current IIS URL redirect setting:
<rewrite>
<globalRules>
<rule name="Http redirect to Https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="localhost:8080" /> <-- the Red one should match above Site Bindings original IP address
</conditions>
<action type="Redirect" url="https://Your-Host-Name/{R:1}" redirectType="SeeOther" />
</rule>
</globalRules>
</rewrite>

Rewrite/Redirect rule in IIS causing trailing slash

I am using 3 domain-agnostic rewrite rules in my IIS. First i redirect from HTTP to HTTPS, then i redirect from naked-domain to www and in the end i remove .php from the URL by rewrite.
However, i see that whenever i access a page which is a subfolder and i access the index page in it, a trailing slash is added in the end of the url. Example:
Root -> subfolder called Items with an index.php in it
URL - www.xxx.net/items/
If i access a file other than index.php inside that folder it doesn't have a trailing slash
Root -> subfolder called Items with 2.php in it
URL - www.xxx.net/items/2
Normally, i wouldn't care but i noticed that google and the crawlers consider www.xxx.net/items and www.xxx.net/items/ as different entities which causes issues.
I know it's something easy but can't pinpoint it right now. Can someone show me how to get rid of this behaviour?
Here are the rules:
<rewrite>
<rules>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Redirect naked domain to www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" negate="true" pattern="^www\." />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Remove PHP" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
</rules>
</rewrite>
After reading a lot about it, it turns out that this is very expected behaviour and you need to factor it in. Basically when you know that you going to get a trailing slash, make sure that your links pointing to this page also include trailing slash in the end so you don't get a redirect.

Regex does not work for {URL} in the URL rewrite in IIS

Using URL rewrite in the IIS on windows 10. I want to check specific url in the IIS.
Below is the condition I need to check.
www.mtl173 or http://www.mtl173 or http://www.mtl173/ or mtl173/
Above all should be valid and should redirect to the given path.
I have checked {URL} so far as following.
<rule name="RuleForSite1" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^(http\:\/\/)?(www\.)?mtl173(\/)?$" />
</conditions>
<action type="Redirect" url="www.mtl173/app1/" />
</rule>
Note: when I test all the URLs in the IIS, all test passed but when I run the same URL from the browser, it shows me the default IIS page and does not redirect me to the app1/.
However, the above does not work and does not redirect the URL to the given path.
Any remediations for the issue I am facing?
Expected result:
When I enter any of the following URL, it should redirect me to the /app1.
www.mtl173
http://www.mtl173
http://www.mtl173/
mtl173/
You can use below url rewrite rule.
<rule name="RuleForSite1" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.mtl173|mtl173$" />
<add input="{REQUEST_URI}" pattern="app1" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mtl173/app1/" />
</rule>
You can learn about server variable from below links:
IIS Server Variables

IIS Rewrite Rule in web.config to redirect HTTPS requests to HTTP

I need to redirect all https requests to http, for example, if someone visits https://www.example.com/another-page/ to http://www.example.com/another-page/
I have the following rewrite rule in my web.config right now, but it's not working correctly. It's redirecting https://www.example.com/another-page/ to https://www.example.com/, so to the root of the site, but instead I want the redirect to stay in the same URL and only rewrite https to http.
<rule name="Redirect to HTTP" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{R:1}" pattern="^onepage/(.*)$" negate="true" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
Any help on changing the above rule so that it only changes https to http, but keeps the full url visited would be greatly appreciated!
I set up your rule, cleaned up a little, and it worked; so this isn't really answering with much new.
Suggestion: Remove the onepage input condition just for testing, as cheesmacfly suggested in the question comment.
Also, try changing the action to {R:1} instead of {R:0}. It shouldn't matter in this case, but I just like using 1 and up, to match the specific capturing group. R:0 means the entire matched string, which always confuses me just a little.
<rule name="Redirect to HTTP" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
One possibility is that your browser has cached a previous attempt of your rules. When the redirectType is Permanent, and you're still developing or testing, the browser often caches a previous rule. Clear your browser cache, and/or remove the Permanent, and/or browse in incognito mode. When done testing, change it to permanent. See number 2 and 3 in this answer: https://stackoverflow.com/a/9204355/292060
Please paste the below code in web.config file.
<rule name="Redirect to http" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTPS_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>

IIS 7 URL Rewrite - CamelCase domain and lower case everything else

I am trying to setup rewrite rules for my site in iis 7 with the URL Rewrite module. If the site name is "WonderfulWidgets"
I want it to always be http://WonderfulWidgets.com.
NOT: wonderfulwidgets.com
NOT: WONDERFULWIDGETS.com
I also want everything after WonderfulWidgets.com to be lower case.
IE WonderfulWidgets.com/best-widgets.
I have accomplished the lower case url rewrite and I have also made it so it will remove any leading www before WonderfulWidgets.com
My problem is my lower case URL rewrite lowers the domain name too. I need help writing the CamelCase domain name that works with rewriting everything else as lower case.
Here's what I have in my web.config:
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^WonderfulWidgets\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://WonderfulWidgets.com/{R:1}" />
</rule>
<rule name="RemoveTrailingSlashRule1" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Default Document URL Rewrite" stopProcessing="true">
<match url="(.*?)/?Default\.aspx$" />
<action type="Redirect" url="{R:1}/" />
<conditions>
<add input="{URL}" pattern="WebResource.axd" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
DNS names are generally treated as case insensitive, and so most (all?) web browsers display the domain name in all lower-case in the address bar. To my knowledge you cannot change this behavior via changing what you return in your HTTP response.
From RFC 4343:
According to the original DNS design decision, comparisons on name
lookup for DNS queries should be case insensitive.
From Wikipedia:
Domain names are interpreted in case-independent manner.
The browsers all seem to prefer lower-case presentation.

Resources