GotKnowHow.com

How to Redirect to WWW of a site using IIS URL Rewrite Module

by


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="http://www.gotknowhow.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>
   </rules>
  </rewrite>
 </system.webServer>
 


0
1

1 Comment

anonymous by Sean Howard on 6/1/2011
Make sure you also include the base domain (yourdomain.com) as a host header in the the bindings of the site.

Add your comment

by Anonymous - Already have an account? Login now!
Your Name:  

Comment:  
Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.


Did this article help you out, share it with your friends:

Ask a Question

140 characters left

Recent Articles