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.