sark9012
sark9012

Reputation: 5747

How can I make the same session function on both "mysite.com" and "www.mysite.com"?

I am having a problem over and over where a member is logged into my site using www. and if he accesses a link without www., the session variables don't carry over.

What's the way to make them both access the same place?

Upvotes: 1

Views: 118

Answers (4)

Treffynnon
Treffynnon

Reputation: 21563

Ideal

Your site should reside on one canonical domain. So you should pick either www. or the top level domain and change all your links so that they point to one web address. It would be wise to switch to setting the domain in a configuration and using that to create web addresses across your application - this way you can easily change the URL later if you wish.

If you are running Apache you can also easily redirect traffic from one domain to the other by adding the following to the .htaccess file of your site:

#enforce the use of the www. subdomain on the sites URL
RewriteCond %{HTTP_HOST}  !^(www.).*$                 [NC]
RewriteRule ^(.*)$        http://www.%{HTTP_HOST}/$1  [R=301,L]

I should also mention that there is a growing movement away from using the www. subdomain as the main "URL" for a site. See: http://no-www.org/index.php

Less ideal

You change the cookie configuration when you set it so that it will work across domains. This is described on the setcookie() manual page with the domain parameter:

The domain that the cookie is available to. Setting the domain to 'www.example.com' will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as 'example.com' will be available to higher subdomains, such as 'www.example.com'. Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.

The only issue with this is that your site will still be accessible via two URLs.

Upvotes: 5

Gustavo F
Gustavo F

Reputation: 2206

when some user access the www.* site, redirect then to the other site automatically

Upvotes: 0

Mario
Mario

Reputation: 36517

Solution 1: Set the cookie's domain to the domain name without the www prefix (this way both requests should be sent with the cookie data).

Solution 2: Redirect everyone using the variant without the prefix to the one with the prefix (e.g. using mod_rewrite).

Upvotes: 4

George Cummins
George Cummins

Reputation: 28926

Modify your server configuration to 301 redirect traffic from 'yourdomain.com' to 'www.yourdomain.com'

Upvotes: 0

Related Questions