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 “www.litemeet.com” and “litemeet.com”. As you can imagine this causes a lot of trouble!


#.htaccess file
RewriteEngine ON

#comment by kyle renfrow:
#http://www.jhuskisson.com/code-tidbits/force-www-in-your-url-using-htaccess
#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]

Leave a Reply

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