Redirect non www to www in IIS using web.config

One common use of URL Rewrite is redirecting http://yourdomain.com to http://www.yourdomain.com. The method below will perform that redirect as well as set a permanent 301 redirect.

Rule 

<rule name=”Redirect to WWW” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^yourdomain.com$” />
</conditions>
<action type=”Redirect” url=”http://www.yourdomain.com/{R:0}” redirectType=”Permanent” />
</rule>

 

Full Web.Config with Rule

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name=”Redirect to WWW” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^yourdomain.com$” />
</conditions>
<action type=”Redirect” url=”http://www.yourdomain.com/{R:0}” redirectType=”Permanent” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

If you dont already have it you can download URL Rewrite from http://www.iis.net/expand/URLRewrite.

Leave a Reply