WWW is dead. Long live WWW!

The discussion over whether one’s domain needs a “www” prefix is lively and on-going. Some are staunchly against; others are stubbornly in favor. (And no one can seem to resist borrowing from Hamlet.)

Seeing I’m never as interested in what’s right as much as what works I present both methods and allow the reader to make up their own mind.

Create an .htaccess file (or open to edit, if there’s already one there) then add your choice of method, replacing “example-domain.com” with your domain:

www is dead

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example-domain.com$ [NC]
RewriteRule ^(.*)$ http://example-domain.com/$1 [L,R=301]

Long live www

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.example-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.example-domain.com/$1 [L,R=301]

Note that both methods employ the 301, or “permanent redirect”, status code. This tells the browser the change is permanent and never to speak of that other domain configuration again.

It’s a best practice to set it first to 302, or “temporary redirect”, confirm everything works, then set it to 301.

If you’re configuring this on a WordPress site, be sure to prevent WordPress from overwriting your rewrite conditions and rules.