Recently I started search engine optimization of my site. I was reading a SEO related blog and I got to know that the Search Engines would penalize my site if both the URLs "http://www.etechplanet.com" and "http://etechplanet.com" will return a "200 OK" code in the HTTP header. Because Search Engine spider thinks that these are two different websites, one with the www prefix, and one without it, which will lead Search Engines to think that the two websites are showing duplicate content. This will hurt the page rankings of the website and also the page rankings will be divided between two websites, and even Search Engines could ban the website for duplicate content.

I looked around Internet and found that I need to implement “301 Moved Permanently” redirect to resolve this issue, means redirect one URL to another and set the http status to “301 Moved Permanently”. There is a way to handle it using IIS, but finally I found a way to implement it programmatically using ASP.Net and C#. In the Global.asax file of the ASP.Net web application, I have added following code inside the Application_BeginRequest event:

void Application_BeginRequest(object sender, EventArgs e)

{

  string authority = Request.Url.Authority;

  if (authority.IndexOf("www.") != 0)

  {

    Response.StatusCode = "301 Moved Permanently";

    Response.AddHeader("Location", "http://www." + authority + Request.Url.AbsolutePath);

  }

}


The above code allows 301 redirects from any entry point to my website. Then I tested my site using "http://www.seoconsultants.com/tools/headers.asp", when I typed "http://www.etechplanet.com" I got a “200 OK” code in the HTTP header, and when I typed "http://etechplanet.com" then I got a “301 Moved Permanently” code in HTTP header. This will ensures Search Engines that both the URLs refer to the same website.