Canonical URLs help you to make your links Search Engine Optimized (SEO). For human it is easy to understand that http://www.contoso.com is same as http://contoso.com. But many search engines will not make this assumption and treat them as two separate entries. This will split the rankings among them and lower the overall relevance of the site.

In IIS7.0 you can use URL Rewrite to solve this problem. The following rule when added in the "Web.config" file in the root of your web site will automatically direct all the people using http://contoso.com to http://www.contoso.com.

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

 

One important thing to remember is to use "Permanent" redirects. This will be helpful if anybody who has linked your page using a non WWW URL. Doing this will direct search engine bot crawls to treat the link as permanently moved. The new URL will be identified as the correct address hence old non WWW URL will not be indexed.

I have just pasted the XML code but the same thing can be achieved using the URL Rewrite GUI which comes with IIS Manager.

Open IIS Manager.

IIS_Manager

Click on URL Rewrite.

Url_Rewrite

Click on Add Rules->Blank Rule.

image

Give a pattern you would like to match, change the “Action type” to “Redirect” and specify the redirect URL. This will add the same content to the config file as shown above.

One important thing to remember is that URL Rewrite should be installed for this to work. You can install X86 version here and X64 here. This module is supported for IIS 7.0 and you should be running IIS 7.0 to take advantage of it. Also if you are working with VWD (Visual Studio for Web Developers) your project should be configured to work with IIS 7.0 and not any other web server.

For details about URL Rewrite module please look here.

Don Raman 
SDET, IIS Team