canonicalization, non-www to www using .htaccess

301 redirecting(permanently moving) non-www to www is something i have done for years, but only today did i realise there was one simple mod_rewrite rule i could use for all my sites.

The old method i used was

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.assertica\.co\.uk*$
RewriteRule (.*) http://www.assertica.co.uk/$1 [R=301,L]

Nothing wrong with this, it works well, but becomes a pain when you have to edit the rule for every domain - easy to make a mistake with all those dots and slashes if you’re a dyslexic monkey like me.

So today i created this nice easy rule that works(at least it should) for all domains without the need to edit. So simple. So for anyone installing a script on multiple domains you no longer need to edit the non-www to www bit for every domain.

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

Be interested to know if anyone finds a setup where the old rule works but the new one doesn’t. Of course for either rule to work you’ll need hosting that supports or emulates Apaches mod_rewrite

Share this article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Digg
  • Furl
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

Recent Posts