I want to add the proper 301 permanent redirect rule in IIS 8.5. I've added the following rules but it is not working.
<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="^(http:\/\/){0,1}(www\.){0,1}([^\.]+)\.([^\.]+)$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.{C:3}.{C:4}" redirectType="Permanent" />
</rule>
Conditions
- If the URL is in HTTPS and contains "www." then NO redirect. Example: https://www.example.com
- If the URL is in HTTP then it should be redirected to HTTPS. Example: http://www.example.com should be redirected to https://www.example.com
- If the URL is in HTTPS but does NOT contain "www." then it should be redirected to HTTPS site with "www." prefix. Example: https://example.com should be redirected to https://www.example.com
- If the URL neither contains HTTPS nor WWW then redirect to HTTPS URL with "www." prefix. Example: http://example.com should be redirected to https://www.example.com
To summarize, every URL should be in HTTPS and should have "www." prefix.
NOTE: I have installed URL Rewrite Module in IIS.
Can anyone please help me to achieve this?
I managed it by adding two URL rewrite rules in
Web.config
file:Redirecting HTTP to HTTPS
All the conditions with
negate="true"
are used for exclusion. Hence all URLs which contains "localhost", "stage", and "dev" are excluded from URL rewrite. You can remove these conditions if not required.Read more about negate attribute at http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module