Nerds at Work

RSS

How to Use .htaccess to Remove www From Inbound URLs

Written by Brian on December 28, 2008 – 6:44 pm -

Simple image of the letters 'www' in gray, on a white background, with shadows.On most modern web servers, you can access the same page with or without the “www” before the domain name. You may be tempted, then, to just let well enough alone and let some people access your site with the ‘www’ and let some people access it without the ‘www.’ 

Well, there are some compelling reasons why that is a bad idea. I’d try to make you feel guilty about that decision, if it weren’t for the fact that I’ve been too lazy up until now to do it myself. Oops.

For those of you that are impatient, I’ll deal with the technical stuff first. Then, we’ll take a look at why you shouldn’t be using the ‘www’ subdomain.

How to Use .htaccess to Remove the ‘www’ From Inbound URLs

This can be accomplished with a simple .htaccess mod_rewrite redirect.

First, let’s take a look at the code as it appears in my .htaccess file.

RewriteEngine On
 
#redirect from www.life-of-brian.com to life-of-brian.com
RewriteCond %{HTTP_HOST} ^www.life-of-brian.com$ [NC]
RewriteRule ^(.*)$ http://life-of-brian.com/$1 [R=301,L]

The first line turns on the mod_rewrite engine. You’ll need this at the beginning of your .htaccess file if you plan to use any rewrites.

The “RewriteCond” line tells the server what to look out for. In this case, the simple regex [^www.life-of-brian.com$] grabs any expression that starts with www.life-of-brian.com. The remainder of the expression – such as the trailing part of the URL – is stored in the variable $1.

The “RewriteRule” line tells the server where it should send the user. It rewrites the entire URL, starting with the domain without the www [http://life-of-brian.com] and then it appends the trailing portion of the URL [$1].

How do you adapt this for your own site? Simple.

In the RewriteCond, change www.life-of-brian.com to your own domain (including the www). Then, in the RewriteRule line, replace http://life-of-brian.com wth your own domain.

It’s also a good idea to wrap the whole snippet with the tags <IfModule mod_rewrite.c> and </IfModule>.

Here’s the entire .htaccess file rewritten for the domain mydomain.com.

RewriteEngine On
 
#redirect from www.mydomain.com to mydomain.com
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]

If you’re using this with an existing .htaccess file, put this snippet at the very beginning. For example, I use .htaccess to create pretty URLs for my WordPress posts. I placed this snippet before the snippet that was automatically generated by WordPress.

Ok. So Why Remove the ‘www’?

Let’s focus on two main reasons why you shouldn’t use – and why should encourage your users not to use – the www subdomain. The first reason has to do with form and semantics, while the second one might have benefits in terms of SEO and SERP.

The Semantics Argument. If you surf around web design blogs, you’ll see a lot of talk about coding semantically correct and standards meeting sites. There’s a belief that doing things the right way is inherently good – even if there may not be a lot of hard evidence that you benefit from it.

Likewise, there’s an argument to be made for the disuse of the ‘www’ subdomain. It is no longer needed, it is depracated, and it should therefore go by the wayside. I stumbled on the site no-www today, and found a great quote to summarize this position:

Succinctly, use of the www subdomain is redundant and time consuming to communicate. The internet, media, and society are all better off without it.

In most cases, there is no need to use the www subdomain. Your browser and the web server assume that you’re looking for web content – so they serve it up automatically. Including the ‘www’ is therefore redundant.

Since the ‘www’ is no longer necessary, it just seems like the right thing to do to not use it. You should therefore gently suggest to your users that they stop typing in those letters for no reason.

The SEO Argument. If you want visitors to find your website through a search engine, you know at least a bit about SEO (Search Engine Optimization) and SERP (Search Engine Results Page) placement. You know that things like links to your site help improve your SEO and SERP – and ultimately bring more users to your site.

So what happens if the search engine thinks that www.mydomain.com/landing-page.html is different from mydomain.com/landing-page.html? If the inbound links to your site are split between these two domains, the search engine might miscalculate the number of links coming to your page – thus negatively impacting your SEO and SERP.

The htaccess mod_rewrite redirect that we used above automatically sends the search engine spider to one page – no matter URL the inbound link provided. There’s no chance that the search engine will get confused and consider the two URLs to be separate pages.

Considering it takes only a couple minutes to create an appropriate .htaccess file, I think it’s time well spent if it has even the slightest impact on your SEO and SERP.

Posted in Nerds at Work, Web Design | No Comments »
Tags:

No Comments Yet

You can be the first to comment!

Leave a Comment