Canonical urls With ASP.NET MVC

Please note: this article was written for the original MVC CTP1 preview in 2007 and is horrifically out of date.

The Canonical url of a site is the url that search providers consider to be definitive. For example the sites http://www.example.com/ and http://example.com/ are technically different. Since the urls are different, search providers may consider the two sites to be duplicate content.

This issue can be resolved in ASP.NET MVC by using a 301 Moved Permanently HTTP redirect. Using Application_BeginRequest in Global.asax we can catch urls and redirect them as needed.

In the above code any urls coming in that aren't prefixed with www will have www. prepended to the domain and be redirected to the appropriate url. Of course the above code can be extended to redirect in any arbitrary manner.