FeaturedWeb

.htaccess fix non www pointing to www

Recently I noticed one of my forums sometimes wasn’t saving users choices of remembering them or allowing them to stay logged in. This is due to the cookie thinking www.domain.com and doamin.com (without www) as 2 different sites.

Below are two pieces of code to fix this issue.

 

Note: Be sure to put it at the top of your .htaccess file under “RewriteEngine On” or you’ll notice it won’t work. Also change my-domain.com to your domain. 

Redirect non-www to www

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirecting www to non-www

If you want to do the opposite, the code is very similar:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC] RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

 

Example of one of mine.

htaccess file www to non www etc

Show More

Fiala06

Technology and outdoor enthusiast. Anything from desktops to servers with a side of mountain biking and skiing. Life is good!

Related Articles

Leave a Reply

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

Close
Close