Filed under Development
Using 301 redirects are a useful way to indicate visitors and search crawlers that a page has been moved permanently to another location.
This is especially important, for instance, when you move your site to another domain. Redirecting all the pages on the old domain to your new site will help Google or Bing crawlers to indicate your site has permanently moved avoiding SEO issues.
There is another case where you want to make use of 301redirects to avoid hurting your SEO, this is when you have more than one domain pointing to the same content.
In my case I have two domains: www.josefcobonnin.com and www.josebonnin.com pointing to this blog. I don’t want to eliminate the first domain, but if I don’t do it the search engines will consider the content duplicated and will impact my positioning on the search results. To avoid it we can make use again of the 301 redirects.
The way to implement 301 redirects with ASP.NET is very easy, what we do is to capture the requests we receive and add to the response the HTTP header Location with the new URL.
The next code should be added in the Global.asax file of our ASP.NET projects.
1: void Application_BeginRequest(object sender, EventArgs e)
3: string url = HttpContext.Current.Request.Url.ToString().ToLower();
4: if (url.Contains("http://www.josefcobonnin.com"))
6: HttpContext.Current.Response.Status = "301 Moved Permanently";
8: HttpContext.Current.Response.AddHeader("Location", url.Replace("http://www.josefcobonnin.com", "http://www.josebonnin.com"));
After implementing this short code, all the requests done to www.josefcobonnin will redirect with a 301 to www.josebonnin.com. You can easily test it using the IE9 developer tools and checking the headers returned.