Search code examples
phpsessionurlno-www

PHP - WWW website pre-fix and no pre-fix use different SESSION variables


When visiting my website using the www prefix from the no-www version (which I normally use) I find that I am no longer logged in... Why is this?

Is there a way to have the two versions of the site use the same SESSION variable or at least redirect to one version so as to prevent internal redirects switching users from one version to another (all my redirect use relative paths so I don't think it would be an issue, but I'm inexperienced so I don't know for sure)?


Solution

  • Add the following to your .htaccess file

    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^example\.com
    RewriteRule ^(.*)$ http://www.example.com$1 [R=permanent,L]
    

    Redirecting it to one place (i.e. www.example.com) will help with the SEO and also fix your issue of different session variables as it will always fall under www version.