Redirect and remove www and add https - iis

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.

Related

web.config - use current page protocol (http/https) as a variable in a redirect rule

On my web.config file, first I need to redirect users requesting URL on http to https. Then, afterwards, I need to do further stuff, I need to make sure www is there in the beginning of the URL. When redirecting users from non-www to www, I want to use the https already set on the previous rule. But I want to do it dinamically, I don't want to hardcode 'https://' on the beginning of my rewrite rule.
The goal is: in the future, if for some reason I need to change this rule from https back to http (I don't think I will, but I may need this), I don't have to go on checking all the redirect rules below to change from 'https' to 'http'. If I use a variable (not sure I should call things inside brackets like {HTTPS} as 'variables'), all I have to do is change the first rule and ignore the rest.
Here is an example so things can get clearer; the key issue I am trying to solve is the item where I have written {WHAT TO USE HERE TO MAKE SURE I USE THE SAME PROTOCOL (HTTP/HTTPS) ALREADY DEFINED ABOVE?}:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<!-- The first rule will do a 301 redirect to https -->
<rule name="Redirect to https" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<!-- Now, let's make sure we have www if request does not carry it -->
<rule name="Redirect to www" stopProcessing="false">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" url="{WHAT TO USE HERE TO MAKE SURE I USE THE SAME PROTOCOL (HTTP/HTTPS) ALREADY DEFINED ABOVE?}www.mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
Thank you so much, any help appreciated.
Best way is to use rewrite map to identify protocol. In your case it will be like that:
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="false">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.mydomain.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

How do I enforce www on an IIS hosted website?

My site is hosted on IIS, I need to enforce the browser to use www prefix.
I've installed the Url Rewrite Module and my rule is:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Add WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
from IIS7 URL Rewrite - Add "www" prefix
But I cannot work out how to maintain ssl
You need to capture the protocol in the input:
<rule name="Enforce WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
{C:1} will contain the protocol and {C:2} will have your domain and anything else.
(source)

Infinite Redirect Loop in IIS 7.5

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>

Force HTTPS via URL rewrite in web.config

I'm trying to force HTTPS via a web.config URL rewrite rule for a URL which includes a path.
eg http://my.domain.com/folder must be forced to use https://my.domain.com/folder
I'd also like to force HTTPS for all pages on their relative paths
eg http:// my.domain.com/folder/page.aspx forced to https:// my.domain.com/folder/page.aspx
This is what I have:
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
Try this URL rewrite rule:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<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>
</rules>
</rewrite>
</system.webServer>
</configuration>
Check your casing and the type of quotes you are using. Also, the 2nd input method is invalid - the pattern isn't valid a regex. This rule works for what you want:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

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