iEntry 10th Anniversary RSS Newsletter Advertising
Join the WebProWorld Forum!

Remove The WWW Subdomain

Post to Twitter Post to Facebook

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.

We can achieve this be simply remove the "www" in the URL's on a website. That also makes the URL shorter and maybe easier recognizable. There are two ways to do this; we can use server-side or client-side techniques. The server-side is the best one, but it cannot be used by all, so I provide a JavaScript version as well.

Client-side
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.

< script type="text/javascript" >
var regex = new RegExp("(http|https)://www\.");
var url = location.href;
if (url.match(regex))
{
url = url.replace(regex, location.protocol + "//");
location.replace(url);
}
< /script >

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.

Server-side
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;

if (_Regex.IsMatch(url))
{
url = _Regex.Replace(url, "$1://");
context.Response.Redirect(url);
}
}
}

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.

Implementation
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 section:

< httpModules >
< add type="RemoveWwwModule" name="RemoveWwwModule"/ >
< /httpModules >

Download
RemoveWwwModule.zip (,66 KB)

Comments

Add to Del.icio.us | Digg | Reddit | Furl Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

About the author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

5 Comments

Great code

Thanks for the great information

Great information

Great information that we are going to test and use on some upcoming sites.

Great post

Great info, we just redesigned our chicago law firm site and this info was very helpful when we starting to do some basic programming.

Great article

thanks for the insight into this programming and seo issue.

Great stuff

Great article, it definitly is important to have some good info to remove the www with the best code possible.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 18 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Featured Headline
Fake Chrome OS Screenshots Punk Tech Media
Mystery Blogger Comes Clean
3 comments | 5 hours ago
 
Subscribe to WebProNews


Send me relevant info