Remove the www subdomain from your website

Feb 4, 2007

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 <system.web> section:

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

Download

RemoveWwwModule.zip (,66 KB)

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (8) -

Keyvan Nayyeri
Keyvan Nayyeri
2/4/2007 2:50:35 PM #

Mads,

Good modification on my module Smile

You should have .NET Framework 1.1 installed on your machine so simply use command line compiler to compile your code into .NET 1.1 assembly and install it on server.  There are some drawbacks for Client Side.  I guess search engines can't understand scripts and they may index both URLs.

Good luck Smile

Mads Kristensen
Mads Kristensen
2/4/2007 3:09:19 PM #

Great idea about the command line compiler. I've never thougth about that.
The client-side version works fine, because it will make sure that all will link to the non-www webpage. That way no search engine will know about both urls. It will not fix old links though, but all new ones. So for new websites, the client-side is just as bullet proof as the server-side.

Keyvan Nayyeri
Keyvan Nayyeri
2/4/2007 3:51:40 PM #

I don't think so.  Search Engines do not index URLs that are come from other sites only.  It also indexes all URLs based on some patterns so I *guess* (am not sure) that they may index both URLs.

Mads Kristensen
Mads Kristensen
2/4/2007 4:05:14 PM #

Sure, they follow inbound links as well. Luckily you are in control over your internal links, so you just change them.

 Jonathan
Jonathan
2/5/2007 11:42:15 PM #

Love your site.

Been trying to teach myself ASP.net for some time, and your site of small concise code is awesome.  Thanks!

On to my question, though (which I think is in the same vein as this one):

What if I wanted to redirect all XXXXXX.org and XXXXXX.net directly to XXXXXXX.com automatically.  Right now, I accomplish it via parked domains, but I'd like to have my visitors' url to say XXXXXX.com whether they type www.XXXXXX.com or www.XXXXXX.net or any combination.

Thanks in advance!

Jon Galloway
Jon Galloway
2/14/2007 12:37:17 AM #

It's easier to just go into the Google webmaster tool and set a preferred domain (www.site.com or site.com).

 nstlgc
nstlgc
2/19/2007 11:48:46 AM #

Search engines will still link to the old www.-enabled links, I doubt many of them actually invoke the Javascript on indexed pages, so your client-side method will not work.

That being said, do NOT remove the 'www'-prefix from your domain name. There's a reason why they're there.

oVan
oVan Belgium
8/2/2007 12:12:07 PM #

If you have access to the IIS settings, you can simply create a second website for the "www..."; url in IIS Manager, and change the properties on the "Home Directory" tab to "A redirection to a URL", then type the www-less url in the box. Enable the checkbox for "A permanent redirection for this resource" also, this will update the webcrawlers.
Remember to remove the www-url from the host headers of the www-less website in IIS (if you can still follow me Smile

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.