Automatically Add WWW to All Your URLs

Automatically Add WWW to All Your URLs

It is always a good idea to pick one format for your website URL and stick with it.  If you want to make sure all your URLs have a “www” in front of them then paste the following code at the top of your “.htaccess” file.

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)([^w][^w][^w][^.].*/(www\.)¦.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(/)?([^w][^w][^w][^.].*/(www\.))$ [NC]
RewriteRule ^ http://%4%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]

The above code uses the apache mod_rewrite module to rewrite your URL.  It also makes sure that there is a trailing slash at the end of your URL.  This website is using the above code to make sure all URLs load with a “www” and have the appropriate trailing slashes.