Forcing www. in your url using .htaccess

I found this great tidbit in the comments section of another blog.
The following code will force the www. in a URL.
There are several reasons this is a good idea, but the one I found was that PHP sessions would be different for “” and “”. As you can imagine this causes a lot of trouble!

#.htaccess file
RewriteEngine ON

#comment by kyle renfrow:
#Forces www. to be prepended both on https and http
RewriteCond %{HTTP_HOST} ^litemeet\.com
RewriteCond %{HTTPS} =on
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^litemeet\.com
RewriteCond %{HTTPS} !=on
RewriteRule .* http://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]

One thought on “Forcing www. in your url using .htaccess

Leave a Reply

Your email address will not be published. Required fields are marked *