Introduction
Today I have recevied a notification in Google Analytics saying "Redundant Hostnames" found for your property dotnetmirror.com. Redundant Hostnames means, search engine considers URL's with http://dotnetmirror.com and http://www.dotnetmirror.com (with out WWW and with WWW) differently.
How to Resolve:
To resolve this issue add below code to Global.asax in Application_BeginRequest method.
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://dotnetmirror.com"))
{
HttpContext.Current.Response.Status = "301 Redirect";
HttpContext.Current.Response.AddHeader("Location",HttpContext.Current.Request.Url.AbsoluteUri.
ToString().ToLower().Replace("http://dotnetmirror.com", "http://www.dotnetmirror.com"));
HttpContext.Current.Response.End();
}
From the above code we are replacing http://dotnetmirror.com with http://www.dotnetmirror.com when the request contains http://dotnetmirror.com.