How to Redirect to WWW of a site using IIS URL Rewrite Module
by
Doug
Updated January 21, 2010
I just installed the URL Rewrite Module for IIS 7.0 and started playing around with how to setup rules to create search engine friendly URLs. The first rule I decided to setup was to redirect a site to the WWW sub-domain version when it was not typed in or linked to using the "www.". For instance the URL: gotknowhow.com will automatically redirect to www.gotknowhow.com using a permanent 301 redirect so that you get maximum SEO benefit.
To start using URL Rewrite Module for IIS 7.0 make sure you've already downloaded and installed it, since it's currently not installed by default. Be sure to get the latest URL Rewrite Module (currently version 1.1) here.
Now in your ASP.NET website simply add the following text to the web.config file:
1.
<system.webServer>
<rewrite>
<rules>
<rule name="Add WWW prefix" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" negate="true"/>
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent"/>
</rule>
</rules> </rewrite>
</system.webServer>
2. If you have an older beta version of the URL Rewrite Module, the rule below works, as you'll see you have to put your actual domain name in the rewrite rule:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.gotknowhow\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.gotknowhow.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>