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 “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.
Source: SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7
Works great, except for one thing. It only redirects from the root of the site. So http://mysite.com will go to http://www.mysite.com. So far so good. However, http://mysite.com/about will still go to http://mysite.com/about and not to http://www.mysite.com/about.