https url in browser

Code Samples,Search Engine Optimization

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

9 Oct , 2008  

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:

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 “” with, well,

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

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

	If InStr(requestedDomain, "") Then

		requestedDomain = requestedDomain.Replace("", "")

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

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

        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
            <add input="{HTTP_HOST}" pattern="^$" />
          <action type="Redirect" url="{R:0}" redirectType="Permanent" />

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

, , , , , , ,

21 Responses

  1. Dave Lawlor says:

    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:


  2. Barry Wise says:

    Thanks for that, I should have mentioned it!

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

    You are now aggregated on SEOMasterList.

    Have a nice day …

  4. John says:

    Yes the modules are great on 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 says:

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

  6. Barry Wise says:

    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. Thanks Barry..
    this redirection code is really very helpful for me.

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

  9. Matt says:

    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.


  10. SEO Norwich says:

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

  11. 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. Issues like this is why I use Appache. Thx 4 the info

  13. LYF4CE says:

    One of the easiest ways to redirect using IIS is to just create a new web site. Most people are interested in making the domain the one that answers canonical URLs. Using IIS headers, simply remove the 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 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

    I can see times when this could cause issues with web sites. For instance, if the developers have hard-coded 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.


  14. Mark says:

    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 says:

    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 it returns rather than simply (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?


  16. 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. 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.


  18. FLorida Man says:

    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. 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 says:

    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 says:

    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.

Comments are closed.