当www前缀为url时,会话中断

How do I fix this problem with $_SESSION variables?

Session variables are present when I am on http://www.example.com, and run this code:

echo 'Printing session variables: <br><br><pre>';
foreach ($_SESSION as $key=>$val)
echo $key. ": ".$val. "<br>";
echo '</pre>';

However, if I am on http://example.com, there are no session variables.

How can I make it so that $_SESSIONs work the same, regardless whether www. is prefixed or not?

I would like the site to default to http://example.com, not to http://www.example.com

From The Guest's comment, I found the correct solution to using an .htaccess file to re-write the URI so that www prefix is re-written to the non-www form, which is a slight variation on the top-rated answer here:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L]

Original answer defaults to the www.example.com URI form; above variation defaults everything to the non-www URI form.

Simple answer is add a line to the .htaccess file to force the urls be using either the www. or no www.