Sunday, March 30, 2014

Redirect non www domain to www in asp.net(301 Redirect)

1 comment
In asp.net you can set 301 redirect which means redirecting for example xyz.com to www.xyz.com can be done in Application_BeginRequest in Global.ascx file.

This is important from SEO perspective because for crawler xyz.com and www.xyz.com are two different url and which is harmful for site ranking on search engine.

Code for 301 Redirect in asp.net in Global.ascx.cs file:

  void Application_BeginRequest()
        {
   if (HttpContext.Current.Request.Url.ToString().Contains("abc.com"))
  {
 HttpContext.Current.Response.Status = "Redirect domain.com to www.domain.com";
 HttpContext.Current.Response.AddHeader("Location",
 Request.Url.ToString().ToLower().Replace(
                        "abc.com",
                        "www.abc.com/"));

}
        }


1 comment: