I have the following in my htaccess to force the www in URLs:
RewriteCond %{HTTP_HOST} !^www\\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] >
RewriteCond %{HTTP_HOST} !^www\\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If you're using a port other than 80 (e.g. localhost:8080) you might need to add it to the regex too:
localhost:8080
RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC] RewriteCond %{HTTP_HOST} !^127\.0\.0\.1(?::\d+)?$