Jan
29
PHP Header Redirect & Sessions Fix
Posted by john in apache, programing

One of the sites that I’ve been working on recently for some odd reason would not log users in (or at least not constantly). After looking into it further, I realized it had to do with the PHP function “header” redirecting the user if the login query was successfully.

The problem came when a user came to the site without typing in “www”. The page was redirecting to the site WITH the “www” and the $_SESSION vars were not getting set.

So to force every page to include the “www”, I added the following 3 lines to the .htaccess file and everything seems to be working fine.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]

One Response to “PHP Header Redirect & Sessions Fix”

Jeff Says:

Thanks! This was a huge help. Should have figured this one out on my own though!

[Reply]

Leave a Reply