IT insight blog

Boost IT troubleshooting

non-www to www redirect in web.config

This post is basically for who need to redirect non-www to www url in windows hosting.

In web.config file as below code:
<system.webServer>
<rewrite>
<rules>
<rule name=”Redirect domain.com to www.domain.com HTTP” patternSyntax=”ECMAScript” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^domain.com$” />
<add input=”{HTTPS}” pattern=”off” />
</conditions>
<action type=”Redirect” url=”http://www.domain.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
</rule>
<rule name=”Redirect domain.com to www.domain.com HTTPS” patternSyntax=”ECMAScript” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^domain.com$” />
<add input=”{HTTPS}” pattern=”on” />
</conditions>
<action type=”Redirect” url=”https://www.domain.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
</rule>
</rules>
</rewrite>
</system.webServer>

In above code please note there are two rules used one for http and another for https. To avoid clashing of http and https in same rule, here used two separate pattern.

Hope it work for you guyz…!!

« »

© 2017 IT insight blog.