Permanent Redirect, Url Canonicalization with ASP.NET
What is Url Canonicalization
Matt Cutts describes url canonicalization and the importance of 301 Permanent Redirect in his SEO advice:
"For example, don’t make half of your links go to http://example.com/ and the other half go to http://www.example.com/ . Instead, pick the url you prefer and always use that format for your internal links."
"Suppose you want your default url to be http://www.example.com/ . You can make your webserver so that if someone requests http://example.com/, it does a 301 (permanent) redirect to http://www.example.com/ . That helps Google know which url you prefer to be canonical. Adding a 301 redirect can be an especially good idea if your site changes often (e.g. dynamic content, a blog, etc.)."
My 2 cents: Google definitely considers "example.com" and "www.example.com" as 2 different sites with similar content - so the site can run into duplicate content issue and loose much of its content to supplemental index...
Permanent redirect for Apache server
Is implemented by .htaccess rule which is like so (just googled it :)):
RewriteEngine On RewriteCond %{HTTP_HOST} ^yourdomain.com [NC] RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
ASP.NET implementation
Well, who cares about Apache, anyway ?
The following snippet is ASP.NET implementation that uses global.asax and demonstrates how to configure 301 permanent redirect from inside your code.
protected void Application_BeginRequest(Object sender, EventArgs e) { HttpApplication app = sender as HttpApplication; string domainName = "example.com"; if (app != null) { string host = app.Request.Url.Host.ToLower(); string requestUrl = app.Request.Url.PathAndQuery; if (String.Equals(host, domainName)) { Uri newURL = new Uri(app.Request.Url.Scheme + "://www." + domainName + requestUrl); app.Context.Response.RedirectLocation = newURL.ToString(); app.Context.Response.StatusCode = 301; app.Context.Response.End(); } } }
Please note: regular redirect has HTTP response status code 302 (temporary). Happy redirecting :)
Monday, April 23, 2007 2:16 PM