SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • LINQ (4)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box
ASP.NET Hosting with MS SQL 2008 – Click Here!

301 Redirects For SEO And PageRank!

(Sep 01 2007 - 07:51:58 PM by Timothy Khouri) - [print blog post]
ASP.NET Hosting with MS SQL 2008 – Click Here!

The company that I work for has hired a consulting agency to review our public website and make suggestions for search engine optimization (SEO) and other things. Since I've been in the web marketing field in a previous job, I already have a strong grasp of a lot of the key concepts. But one of the suggestions that they made was something that I didn't know how to do with ASP.NET until now.

301 Redirects in ASP.NET

A "301 redirect" tells browsers and search engines that a resource has been PERMANENTLY moved to another location. This is useful in domain canonicalization (which is a big word that basically means "www" and "non-www" are counted together). So here's the code that I added in my Global.asax file for SingingEels that will ensure that "singingeels.com" will redirect to "www.singingeels.com" as I want it to. Not only will it redirect, but it will add the appropriate "301" header which will tell Google, Yahoo, MSN, ASK.com (and any other search engine) to credit any links or info that they have to the main URL.

Here's how I did it in C# in my Global.asax file:

void Application_BeginRequest(object sender, EventArgs e)
{
   if (this.Request.Url.Host == "singingeels.com")
   {
       UriBuilder redirectToBuilder = new UriBuilder(this.Request.Url);

       redirectToBuilder.Host = "www.singingeels.com";
       
       this.Response.Status = "301 Moved Permanently";

       this.Response.AppendHeader("Location", redirectToBuilder.ToString());
   }
}

I intentionally do not do a "Response.End" after that in case the browser or search engine doesn't know how to do the redirect. But what this does is it makes it so ANY request that comes to my application under the "singingeels.com" domain will be moved to "www.singingeels.com". I also intentionally chose to only redirect that one domain for now.

  • Sep 04 2007 - 01:51:31 PM Timothy Khouri

    I've made a few changes to my above code. For one thing, I changed from an "if" to a "switch" statement because I forgot to include the other DNS names that resolve to Eels (basically mispellings of the name).

    The other thing I've changed is "redirectToBuilder.ToString()" is now "redirectBuilder.Uri.ToString()". This makes it so that port 80 is not (needlessly) specified.

    One of the other developers noted that Safari was showing port 80 in the URL because of the way I was doing the 301 redirect.

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Blog Archives

  • November 2008 - (1)
  • October 2008 - (2)
  • September 2008 - (2)
  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Jan 05 2009 - 08:24:50 PM - (0.0624984)