SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7

Some readers have emailed me asking why I’m only writing about canonical URL and redirect issues for the apache/linux platform and haven’t given any advice on how to fix these issues on Microsoft Windows IIS/ASP.NET servers. So in the interest of equal time, I figure I had better present fixes for both old and new versions of IIS. In IIS 6 it can be corrected with global.asax, but with IIS 7 Microsoft added URL redirect support to the web.config file.  First a URL redirect fix for the older versions of ASP.NET on IIS 6: 

 

Canonical URL Issues In Microsoft Windows IIS 6

IIS 6 (Microsoft’s Internet Information Server webserver) doesn’t have URL redirection built-in, but you can still add it on your own. Here’s the quickest and easiest way to fix the most common canonical URL issue, where both of these URLs return the same (duplicate) content:

http://yoursite.com

http://www.yoursite.com

The quickest and easiest way I’ve found to correct this is to add the following code to your global.asax file. The global.asax file is the ASP.NET application file which gets executed before anything else every time a page is requested on your webserver. Just edit the following code and replace “yoursite.com” with, well, yoursite.com.

Sub Application_BeginRequest(ByVal sender as Object, ByVal e as EventArgs)

Try
	Dim requestedDomain As String = HttpContext.Current.Request.Url.ToString().toLower()

	If InStr(requestedDomain, "http://yoursite.com") Then

		requestedDomain = requestedDomain.Replace("http://yoursite.com", "http://www.yoursite.com")

		Response.Clear()
		Response.Status = "301 Moved Permanently"
		Response.AddHeader("Location", requestedDomain)
		Response.End()

	End If

Catch ex As Exception
	Response.Write("Error in Global.asax :" & ex.Message)
End Try

End Sub

The first thing this code will do is assign the requested URL to a string called requestedDomain.  I then use an If … Then conditional to check and see if the string contains http://yoursite.com.  If it does, I simply replace it with the www version.  Next I present a 301 Redirect header to the visitor (which may be a search engine, such as Google) and redirect the request to the new URL, which is the www version.  I also use some simple Try … Catch error checking; you can use something more complex if you wish.

 

Canonical URLs And 301 Redirects With IIS 7

With the latest release of Microsoft Windows IIS 7 webserver, URL redirect rules have been enabled in the web.config file. The following code comes from Carlos Aguilar Mares’ blog on iis.net, and demonstrates how to redirect using the new ASP.NET web.config rules. (His website also includes other code samples to demonstrate using these rules to create search engine friendly URLs, and more – check it out).

UPDATE: Pageoneresults pointed out to me that iis.net isn’t even using this technique … they didn’t fix their own canonical URL issues!!

And thanks to Dave Lawlor for reminding me that the URL rewrite module is not included by default; download links are available on iis.net

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

It is important to note both of these code samples demonstrate how to redirect incoming requests from http://yoursite.com to http://www.yoursite.com. If you want to make http://yoursite.com the canonical URL, you’ll have to reverse the logic.

Tags: 301 redirect, asp.net, canonical urls, IIS, Microsoft Windows, search engines, seo, Web Servers

Similar Posts:

Comments

21 Responses to “SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7”

  1. Dave Lawlor on October 10th, 2008 1:11 pm

    Good article, but you might want to mention that on IIS 7 they need to download and enable the URL Rewrite module for that piece of code to work. It is not enabled with IIS 7 be default, nor does it even come included with Server 2008. You can get the module download at:

    http://learn.iis.net/page.aspx/460/using-url-rewrite-module/

    Dave

  2. Barry Wise on October 10th, 2008 1:12 pm

    Dave;
    Thanks for that, I should have mentioned it!

  3. Edward Beckett on October 14th, 2008 3:54 am

    Barry … Excellent post – I find the content on your site … Superb.

    You are now aggregated on SEOMasterList.

    Have a nice day …

  4. John on October 15th, 2008 12:21 pm

    Yes the modules are great on iis.net page
    these modules is very new to me so i have a question.
    The second code from Dave’s link is it a redirection module? please give us some more posts on that barry

  5. Barry Wise on October 15th, 2008 12:23 pm

    @John Yes, that is the IIS 7 redirect module download page link.

  6. Barry Wise on November 3rd, 2008 1:12 pm

    Dave;
    That is a question better answered in a forum on IIS issues, since there are so many variables at play and since I have no idea how the rest of your server is configured.

  7. anchal@SEO seomega corp on November 25th, 2008 11:07 am

    Thanks Barry..
    this redirection code is really very helpful for me.

  8. SEO Strategies on December 17th, 2008 8:10 am

    Great code, thanks for that It will be useful for my all sites.

  9. Matt on March 11th, 2009 12:58 pm

    Thanks for the Article Barry,

    Is there a reason to use the global.asax file over the 301 redirection available in IIS 6.0 when configuring the home directory?

    Would you use the global.asax file because some search engines treat this 301 as a 302? Or is it an issue of load on IIS.

    Thanks!

  10. SEO Norwich on March 18th, 2009 12:11 pm

    Great stuff barry, I predominatly work with Apache but this has been a great insight to how this is done in IIS

  11. otimização de sites on March 20th, 2009 3:55 pm

    Great tips!
    I always used Redirects in .htaccess files or setting up the server.
    Thanks for the info.

    I´ve already bookmarked and subscribed the feeds.

  12. Tyler Cordaro on March 26th, 2009 11:36 am

    Issues like this is why I use Appache. Thx 4 the info

  13. LYF4CE on March 28th, 2009 6:52 pm

    One of the easiest ways to redirect using IIS is to just create a new web site. Most people are interested in making the http://www.yoursite.com domain the one that answers canonical URLs. Using IIS headers, simply remove the yoursite.com domain from the primary web site.

    Create a new web site and make sure during the setup that you choose the path that your site’s files are located. Once the site is setup, open its properties, click the Advanced button and enter yourdomain.com as the header. Click the Home Directory tab and check the radio “A redirection to a URL” and the checkbox “A permanent redirection for this resource”.

    All the files in your website will now automatically and permanently redirect to http://www.yourdomain.com.

    I can see times when this could cause issues with web sites. For instance, if the developers have hard-coded yourdomain.com into the web code and the code misbehaves during the redirect. Albeit, if you’re coding like that you should have your hand slapped like a Catholic nun with a ruler, it can happen. From my experience, if this doesn’t work for your web site you have other issues that should be dealt with anyway because things aren’t being done right somewhere in the code.

    Again, easy fix. Each situation will be unique and should be assessed thoroughly before implementing a solution. Sometimes the simple solutions are the best; sometimes developers need a swift kick in the pants.

    Cheers!

  14. Mark on May 6th, 2009 1:09 pm

    Thanks for this. I just inherited a site on IIS 6. I’ve only worked with Apache and .htaccess for 301s in the past.

    My question is, I have also found a tutorial that suggests 301 is best done through IIS 6 control panel rather than through the global.asax file.

    We have an outsourced network admin who knows nothing of SEO and so I need help figuring out the right way to 301 in IIS 6.

    Sorry for asking a potentially dumb question.

  15. Jordan on July 21st, 2009 12:46 am

    Hi Barry,

    Thanks for the article. I’ve implemented your IIS 6 solution on my site. It works well, except that if I type in http://mysite.com/ it returns http://www.mysite.com/index.html rather than simply http://www.mysite.com/ (i.e. I don’t want to have the “index.html” portion visible in the address).

    Is there some additional code that can be added to the global.asax file to handle this?

    Thanks!

  16. Social Maximizer on July 31st, 2009 8:31 am

    I did as you wrote but have got the error: Error in Global.asax: followed by no error message. Wonder where I’ve gone wrong?

  17. Mark | SEO Malta on October 7th, 2009 3:43 am

    If I understand correctly he said you can use web.config instead of the global.asax on IIS7. I tried implementing the changes in web.config to sort canonicalization for SEO and worked fine for me.

    Thanks,
    Mark

  18. FLorida Man on March 22nd, 2010 6:56 am

    Thanks for this as have been trying to find the fianl and correct way for doing this. My host said they could not do it and I needed to do it in code, this is a great clean example of a way to do as this was the last of my canonicalization mission to resolve on my site.
    Thanks Barry – Good Post mate

  19. Piscina de Vinil on April 17th, 2010 3:27 pm

    Great stuff barry, I predominatly work with Apache but this has been a great insight to how this is done in IIS

  20. umesh chand on July 12th, 2010 7:11 am

    I have used IIS 7 url rewriting for Canonical problem, its working but i have a master page on which i have placed a login control and when i tried to login it simply refreshes the page and nothing happend on default page. but when do the same thing from other page login works.
    I removed the url rule then check and its working again, i m getting the problem why its not working on defalut.aspx page.
    There is also a button on master page.On clicking that button i just passed some querystring and redirect to other page and it also not working just refreshes the page.
    After removing the canonical rule all is well again.
    Remember only on default.aspx page this creates a problem except this page everything works fine on other pages.Please help me to solve this issue

  21. stephen seo on July 7th, 2012 2:22 am

    The canonical tag is a part of the HTML header of a web page. It should be placed between the opening and closing sections of the source code. By using canonical tag through you can solved web page duplicate issue.