asp.net - How to redirect all non-www URLs to https://www. in IIS? -
i want add proper 301 permanent redirect rule in iis 8.5. i've added following rules not working.
<rule name="redirect top domains non-www 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 url in https , contains "www." no redirect. example: https://www.example.com
- if url in http should redirected https. example: http://www.example.com should redirected https://www.example.com
- if url in https not contain "www." should redirected https site "www." prefix. example: https://example.com should redirected https://www.example.com
- if url neither contains https nor www redirect https url "www." prefix. example: http://example.com should redirected https://www.example.com
to summarize, every url should in https , should have "www." prefix.
note: have installed url rewrite module in iis.
can please me achieve this?
i managed adding 2 url rewrite rules in web.config
file:
- for redirecting non-www https://www.{domain}.com/...
redirecting http https
<rule name="redirect top domains non-www 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>
all conditions negate="true"
used exclusion. hence urls contains "localhost", "stage", , "dev" excluded url rewrite. can remove these conditions if not required.
read more negate attribute @ http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
Comments
Post a Comment