Fix: Different session with www and without www

Tags: web, apache, iis, fix | Comments (0) | Posted on Sunday 27th of September 2009 07:02:16 AM

What's wrong?

When your website has www such was http://www.sprklab.com, its actually a different sub-domain then http://sprklab.com. So you end up having 2 separate cookies for the same website! In fact search engines may even consider it as 2 different websites! So if you logged in with http://www.sprklab.com you can log in as one user and then go to http://sprklab.com and login with another.

Attempted Fix

You can try to set the cookie to support all sub-domain by doing ".sprklab.com"
I dont know why but django session was not working in IE when I did this but worked fine in Firefox.

Proper Fix

Just don't have a site without the www, if the user tries to go to http://sprklab.com, just redirect them to http://www.sprklab.com
If you look around the web this seems to be the common implementation. Just try going to http://google.com, http://facebook.com, http://yahoo.com. They will all redirect you to their www subdomain.

For Apache Servers

Just add this to you .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]

Click here to add your own comment!