Share via


Tip#25: Did you know... You can have canonical URLs and Redirects with IIS 7.0

Canonical URLs help you to make your links Search Engine Optimized (SEO). For human it is easy to understand that https://www.contoso.com is same as https://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 https://contoso.com to https://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="https://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

Comments

  • Anonymous
    November 19, 2008
    PingBack from http://blog.a-foton.ru/index.php/2008/11/20/tip25-did-you-know-you-can-have-canonical-urls-and-redirects-with-iis-70/

  • Anonymous
    November 19, 2008
    Hi, We have a application running in the HTTPS . Will this thing work in SSL . Please give me some idea. Thanks , Thani

  • Anonymous
    November 20, 2008
    This approach can be used with HTTPS as well. For example if you have a certificate issued for domain www.contoso.com but want your site to be discoverable when users type http://contoso.com or http://foobar.com then you could have a rule that redirects to https://www.contoso.com <configuration>  <system.webServer>    <rewrite>      <rules>        <rule name="WWW Redirect" stopProcessing="true">          <match url=".*" />          <conditions logicalGrouping=”MatchAny”>            <add input=”{HTTPS}” pattern=”off” />            <add input="{HTTP_HOST}" pattern="^www.contoso.com$" negate=”true” />          </conditions>          <action type="Redirect" url="https://www.contoso.com/{R:0}" redirectType="Permanent" />        </rule>      </rules>    </rewrite>  </system.webServer> </configuration>

  • Anonymous
    March 10, 2009
    Any tips for doing this in IIS6? Thanks, Mike

  • Anonymous
    October 26, 2011
    Nice tips to note. Thanks, Thani