Forcing the correct domain name

Nothing new here but a quick note to document how I prefer to handle forcing browsers to see the a website using the preferred domain name. In the virtual server setup place the directives to have the virtual host respond to all the names (i.e.,, Then use the rewrite engine to change the url to be the preferred URL:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com        [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*)$1 [L,R]