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

by 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>
 


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.

Related Posts


If you've recently moved a Visual Studio.net web site project (wsp) or web application project(wap) to deploy on Internet Information Services 7 (IIS7) you may have discovered that your Tiny_MCE text editor no longer is showing up and getting the pop-up...  more »

After setting up a new Windows 7 computer with IIS 7.5 and Visual Studio 2010, I tried to start my ASP.NET 4.0 website using the Local IIS web server. However, right off the bat I was hit with the following IIS error message: HTTP Error 500.21 - Internal...  more »

On a number of a occassions I've run into the following ASP.NET / IIS7 error after uploading my newly built Visual Studio.net web project site into IIS (where the site had previously been working fine): 'Timeout expired. The timeout period elapsed prior...  more »

SQL Server Reporting Services can be a pain in the arse to set up correctly for the first time. Even after you've got things running correctly, you can sometimes run into issues, which is exactly what happened to me recently. I had setup my local...  more »

After running a ASP.NET website on IIS 7.5 for the first time on a Windows 7 computer, I was faced with the following error message: Login failed for user 'IIS APPPOOL\ASP.NET v4.0'. Description: An unhandled exception occurred during the execution of the...  more »

If you recently bought a new Dell computer, and then opened up Internet Explorer you may have been automatically redirected to (Dell's) dell.msn.com web page in Internet Explorer, instead of being directed to www.msn.com. This hijacked dell.msn.com URL...  more »

Here's how to install Internet Information Services (IIS7) on a Windows 7 (or Vista) computer so that ASP.NET websites will run on the IIS7 web server. First, you will want to make sure that you are signed into an account with Administrator access on your...  more »