简体   繁体   中英

Session not maintained between mysite.com and www.example.com

I am using a session to maintain some variables between different pages on my site. While it works fine when I move from www.example.com/a to www.example.com/b, the session variables are lost if I move from example.com/a to www.example.com/b

Any idea how I can make sure the session is maintained irrespective of www or no www?

The website appends a www as it navigates to different pages but the pages are accessible without that as well. So if someone is manually typing in an address and omit that part, and then navigate to page b (by clicking on a link or submitting a form), the session variables are lost.

The reasonable way to do it is by redirecting example.com to www.example.com ; by that you make sure that all your visitors are in www.example.com , and crawlers won't index two pages with the same content.

You do that by sending a 301: Moved Permanently to users accessing example.com (assuming you use apache, you have to add this to your .htaccess ):

Redirect 301 http://example.com/ http://www.example.com/

Second version:

 Redirect 301 http://example.com/(.*) http://www.example.com/$1 

Sorry, this one is right:

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

Taken from here http://www.webconfs.com/how-to-redirect-a-webpage.php

你需要确保使用session_set_cookie_params将cookie设置为.example.com而不是www.example.com,特别是参数3 - 文档中的域

The session ID is stored in a cookie. Cookies don't transfer over from subdomain to subdomain (eg www.somesite.com to somesite.com). You can change the cookie params with session_set_cookie_params() or in php.ini

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM