Printed September 8, 2010 from B&T's Tips & Scripts
http://tips-scripts.com
Forcing or eliminating the WWW.
If you want your website to always use the WWW. prefix (like www.domain.com) then this htaccess code is your answer.
You may want to force the WWW because you like it that way, or because you want consistency for search engines.
What ever the reason, use this htaccess code and you will always have the WWW domain prefix used for your website.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The way this works is if the domain name does not start with WWW then the request is rewritten with the WWW.
The WWW is really just treated as any other subdomain.
So if you have other subdomains (like sub.domain.com) where you do not want the WWW added (converted to www.sub.domain.com), then you need to include another RewriteCond line for each one.
Or if you want to always eliminate the www prefix, use this htaccess code.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]