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.
I put this in my blog’s htaccess file and it works like a charm!
Thanks for this tip. I noticed though that it worked on every site, except for my server’s default site (when configured with multiple virtual hosts).
How would one go about changing this so it would work for both HTTP and HTTPS?, as I see that HTTP is hard coded into it.