Many websites have two points of entry for each individual page – one with www and one without. It means that if other websites link to yours, they might link to the same page using two different addresses. If you make sure that all incoming links point to the same address, you also make sure that various page ranking algorithms of the different search engines give the page full credit for each incoming link.
Add this script tag to the header of your web pages. This is the solution I have implemented here on this blog, because I cannot use the server-side approach.
var regex = new RegExp("(http|https)://www\.");
var url = location.href;
url = url.replace(regex, location.protocol + "//");
The reason why I can’t use the server-side approach is because dasBlog is a .NET 1.1 project and I haven’t got Visual Studio 2003 installed on my machine. In with the new, out with the old and face the consequences.
The server-side version uses an HttpModule to filter the incoming requests and redirects them to the URL without “www”. I’ve modified a little upon the excellent HttpModule of Keyvan Nayyeri.
private static Regex _Regex = new Regex("(http|https)://www\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private void context_BeginRequest(object sender, EventArgs e)
HttpContext context = (sender as HttpApplication).Context;
if (context.Request.PhysicalPath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
string url = context.Request.RawUrl;
url = _Regex.Replace(url, "$1://");
For discussions about the use of 302 vs. 301 redirections, please see the comments on Keyvan’s post for details and make your own decision.
Download the RemoveWwwModule.cs below and put it the App_Code folder in the root of your website. Then add the following lines to the web.config’s <system.web> section:
<add type="RemoveWwwModule" name="RemoveWwwModule"/>