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:

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.

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


One thought on “SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s