Welcome to MSDN Blogs Sign in | Join | Help
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 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

Posted: Thursday, November 20, 2008 1:57 AM by WebDevTools

Comments

MisterFantastic said:

Hi,

We have a application running in the HTTPS . Will this thing work in SSL . Please give me some idea.

Thanks ,

Thani

# November 20, 2008 12:00 AM

Ruslan said:

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>

# November 20, 2008 1:25 PM

Preqin said:

Any tips for doing this in IIS6?

Thanks,

Mike

# March 11, 2009 6:16 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker