Infinite Redirect Loop in IIS 7.5 - iis

I am trying to add redirect rule 'www' when the request comes without the 'www' for the site. ie. http://example.com to http://www.example.com
Here is what the rule looks like:
<rewrite>
<rules>
<rule name="Add www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com" />
</rule>
</rules>
</rewrite>
Once i add this rule, the site goes to infinite loop and it just error out (page can't be displayed error message). The server is brand new and it might be missing some redirect components (if there is one). I did install URL Rewrite component and added rule in it.
Any suggestions?
Thanks.

You have not chosen a pattern syntax and so you using the default regular expression syntax. As a result your pattern matches both example.com and www.example.com and causes an infinite loop. Try this:
<rewrite>
<rules>
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>

Related

How to add rules in Url redirect in iis 10

I want to write redirect rules in IIS 10. I googled it and spent half of my day figuring out but no luck so I am posting it to get some solution.
https://testing.app.com/apptest should redirect to https://testing.app.com/apptest/account/login
https://testing.app.com/apptest/ should redirect to https://testing.app.com/apptest/account/login
https://test-apptest.testing.app.com/ should redirect to https://test-apptest.testing.app.com/account/login
https://test-apptest.testing.app.com should redirect to https://test-apptest.testing.app.com/account/login
But when user types url https://testing.app.com/apptest/account/login or https://test-apptest.testing.app.com/account/login then it should not redirect anywhere and it should stay as it is.
<rewrite>
<rules>
<rule name="Test1" stopProcessing="true">
<match url="account/login" />
<action type="None" />
</rule>
<rule name="Test2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testing.app.com" />
<add input="{REQUEST_URI}" pattern="^/apptest" />
</conditions>
<action type="Redirect" url="https://testing.app.com/apptest/account/login" appendQueryString="false" />
</rule>
</rules>
</rewrite>
We just add an anchor point to the regular expression so that precisely matches the segment ‘/apptest’.
Updated
<system.webServer>
<rewrite>
<rules>
<rule name="MyRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^((/apptest)?)/?$" />
</conditions>
<action type="Redirect" url="https://{http_host}{C:1}/account/login" />
</rule>
</rules>
</rewrite>
</system.webServer>
Explanation
Since the hostname changed and will subsequently be appended in the redirection URL, I replace it with {http_host} server variable to follow it. Besides, {Request_URI} will return the URL path and {C:1} will return either "/apptest" or "". therefore I append it into the redirection URL.
The meaning of every server variable.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule
Finally, please don’t forget to install the URL Rewrite extension before applying the rules.
https://www.iis.net/downloads/microsoft/url-rewrite
Here is a quick reference of the regular expression from Microsoft documentation.
https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Feel free to let me know if the problem persists.

iis url rewrite - how to match pattern starting with "?"

i'm trying to force a url redirect (with the url rewrite module but for some reason it doesn't seem to be willing to match..)
my use case is as follows:
url: http://somesite.domain.com/?test=4
Now I'm trying to redirect this when it matches ?test=4 to http://somesite.domain.com/subsite/?test=5
I've tried this in multiple ways aleady by having a matching pattern of ^\?test=4 (on both the match url or the condition)
For some reason tho it isn't willing to trigger.
Is there something obviously wrong with a matching regex pattern of \?test=4 or should this work?
Full web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="^\?test=4" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\?test=4" />
<action type="Redirect" url="/subsite/?test=5" appendQueryString="true" />
<conditions>
</conditions>
</rule>
<rule name="2" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAny">
</conditions>
<action type="Redirect" url="/subsite/?test=5" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I'm fairly now to the url rewrite module so I'm going a bit by trial and error..
Any tips would be nice!
As far as I know, the url match will not match the query string. So your url rewrite rule will not work.
If you want to check the querystring, I suggest you could use IIS url rewrite querystring condition.
More details, you could refer to below url rewrite rule:
<rule name="MatchQueryString" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="test=4" />
</conditions>
<action type="Redirect" url="/subsite/?test=5" appendQueryString="false" />
</rule>
Result:

URL Rewriting the path in IIS

I have a site that uses two characters in the URL path to determine the initial language e.g. https://my.company.net/Monitoring/gb displays in the English language. I want to setup a redirect for these two display the full culture code e.g. https://my.company.net/Monitoring/en-GB
This is the rule that I've tried:
<rewrite>
<rules>
<rule name="Monitoring gb rewrite" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://my.company.net/Monitoring/gb" />
<conditions />
<serverVariables />
<action type="Redirect" url="https://my.company.net/Monitoring/en-GB" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I expected https://my.company.net/Monitoring/gb to redirect to https://my.company.net/Monitoring/en-GB however this rule does not have any effect: the browser URL stays at https://my.company.net/Monitoring/gb.
How can I rectify this?
You could use below url rewrite rule.
<rule name="gb to en-gb redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="ww.sample1.com" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="Monitoring/gb" />
</conditions>
<action type="Redirect" url="https://www.sample1.com/Monitoring/en-GB" />
</rule>
Note: use your hostname instead of the www.sample1.com.
The redirect failure was due to browser caching. The redirect works once the cache is cleared.

Redirect and remove www and add https

This rule seems to work fine, but when I go to https://www.example.com it doesn't remove the www.
However, if I go to http://www.example.com it works as expected.
Why is it not redirecting from https?
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$"/>
</conditions>
<action type="Redirect" url="https://example.com{PATH_INFO}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Per my comment above, your rule actually worked for me in the scenario that you described. However, I do not think that it handles the case when a request is made to an insecure url that does not contain www in the hostname. For example, http://example.com/blah/ would not be redirected. The following rule below handles that case as well:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Remove WWW and Ensure HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://example.com{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The rule checks for a hostname starting with www or an insecure request. If either condition is met, the user is redirected.

Best way to redirect websites in IIS 7

What is the best way to make sure all pages hit on a website hosting in IIS 7 are redirecting with the www. sub-domain...examples below:
http://site.com ==> http://www.site.com
or
http://site.com/somepage.html ==> http://www.site.com/somepage.html
This rule will redirect (301 Permanent Redirect) all requests to http://site.com/somepage.html into http://www.site.com/somepage.html (will work if domain name = site.com):
<system.webServer>
<rewrite>
<rules>
<rule name="Force www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^site\.com$" />
</conditions>
<action type="Redirect" url="http://www.site.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
This one will do the same but will redirect from ANY SUBDOMAIN to www (e.g. hello.site.com => www.site.com):
<system.webServer>
<rewrite>
<rules>
<rule name="Force www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.site\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.site.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
NOTES:
You will need URL Rewrite v2 module to be present (should work with v1.x as well, but I cannot test it -- only v2 around).
(Obviously) your site should accept those non-www subdomains.
This rule need to be placed in web.config in website root folder.

Resources