Fix canonical redirect issue on ASP NET in a shared host (Rackspacecloud)

by Xavier Comments: 2

I delivered an seo optimization project a few days ago and one of the issues that needed to be fixed was the canonical redirection issue. For those of you who don’t know exactly what it is (but probably are here because you want to implement a fix for it), let me explain.

– You have a site, i.e. www.xaviermorera.com
– You have a page: www.xaviermorera.com/mypage.html
– If you type xaviermorera.com/mypage.html, the page still displays but the “www” is not added

That is canonical redirection, both urls work and the “www” is not added. So why it is a problem? Because Google or other search engines think it is actually two pages instead of one. Which it is funny because you have canonical redirection on one end and page canibalization on the other!

So, how to solve?
– One (expensive) option is to install an isapi filter on your IIS
– Or have a dedicated host and configure via the IIS manager

But none of them work in a shared host!

So, here is the solution you’ve been waiting for: (this post is specific for ASP NET)

In the Application_BeginRequest in your global.asax, just type a code to look for the “www”, and if it is missing simply add it. The following code will do the trick:

if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith(“http://xaviermorera”))
{
string newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace(“http://xaviermorera”, “http://www.xaviermorera”);
Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”, newUrl);
}

I did it for http://www.softwaresynergy.com and it worked fine 🙂