Adding WWW to domains, and Apache Rewrite Rules (mod_rewrite)

Browse to http://google.com. Then look at the address bar. You're not really at http://google.com. You've been redirected to http://www.google.com. Try the same on w3c, Facebook, Sydney Morning Herald etc.

Why WWW?
Why do all these sites redirect you to a www form? Well, the main reason is because it is advantageous to have a canonical URL, and, if your have to choose one URL, you might as well go with what people seem to expect, which is to include a 'www'.

What's so great about having one canonical URL?

  • Cookies: if your users can access the site at www.domain.com and domain.com, you can end up with some horrible cookie and session problems depending on the browser and web framework (behaviour is different between Firefox and IE). Stay tuned for another post with more details on this.
  • Certificates for HTTPS: certificates are usually for a single domain. If your site is available with and without 'www', your site will need a certificate for each or a multi-domain certificate (ie, more money and config).
  • Caching: if you have two URLS, any HTTP caching will only be half as effective
  • SEO: your page rank may be split between links to both possible URLs (though Google Webmaster tools seems to let you combine it)

How?
Right so, now you're probably just hoping there is an easy way to implement this forced 'www' business! Well the good news is that it's quite easy if you're using Apache with mod_rewrite. I googled around to try and find some good rules, but the ones I found were tied to a single hard coded domain (no good for me where I have multiple domains pointing to the same server for different countries). See below for what I came up with. It seems to work quite well. You can put it in your virtual host configuration file or even .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]

Line 1: Are you coming to the site without www. at the start of the host? [NC] means ignore case.

Line 2: Does your domain comprise of letters and dots (this means that going to the IP address will not fire the rewrite rule). Grab the domain in a capture group.

Line 3: Rewrite the URL with a www at the front, and keep the hostname from the previous condition (%1) and the path after the domain ($1). Use a status code 301, to tell the client that this is a permanent redirect.


Actions

Information

6 responses to “Adding WWW to domains, and Apache Rewrite Rules (mod_rewrite)”

29 02 2012
Nidheesh (19:09:30) :

Thanks

29 02 2012
Nidheesh (19:10:29) :

Its Working

8 03 2012
David (08:36:53) :

Hi,

It will not work for me, could it be because I am using .co.uk domains and not .com?

11 05 2012
Chris V (21:38:13) :

Nice way to add www prefix - but it wouldn't work for me until removed the '/' from ^/(.*)$ to make it ^(.*)$.

Thanks

11 05 2012
Chris V (22:09:10) :

Actually, I hit problems with other subdomains (dev., etc) - I found this works:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

from http://stackoverflow.com/questions/2621089/#answer-2720415 (though I think it wouldn't work with .co.uk!)

6 12 2012
ルイヴィトン (15:13:11) :

It's perfect time to make some plans for the longer term and it is time to be happy. I've read this publish and if I may just I want to suggest you some attention-grabbing things or suggestions.
Perhaps you can write next articles relating to this article.
I wish to learn even more issues approximately it!