Http host / cookie problem?


I’ve just registered a domain for my site and have noticed that it sets different cookies depending on whether you have www in front of the url or not
ie I can be logged on as two different users in the same browser, the one logged into and the other into

I had a look around and most sites seem to automatically put a www in front even if you type into the address bar.

So i just put a little function at the top of the page to check whether a www is present in the address and if not then redirect accordingly. Is this normal or do i have a problem somewhere along the line?


yes what you have done is OK.

strictly speaking a dns name should have a host (ie www) included but lots of isp create an A-record to allow the www or whatever to be omitted.

You can put this in .htaccess rather than a function at the top of the page.

# force
RewriteCond %{HTTP_HOST} ^www\\.domain\\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

# _or_ force
RewriteCond %{HTTP_HOST} ^domain\\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

They are different domains, as far as which is correct, I think it’s entirely up to you.

Thanks very much, will get that a try…