.htaccess使用mod_rewrite 在头文件redirect之后丢失的SESSION与启动的会话

PHP版本5.4.41 Apache版本2.2.15 Linux版本2.6.32 CentOS 6.6

我有一些代码不会正确redirect。 有很多的代码和configuration,所以我会尽量保持简单。 我的会话在头redirect后不断丢失。 没有错误,没有警告…redirect进展良好。 我session_start(); 和var_dump [$ _ SESSION]在页面redirect后得到NULL。 如果我session_start和redirect之前转储,会话转储罚款。 我猜测这与htaccess mod_rewrites在页面之间放置会话有关,但我不确定如何解决这个问题。 我一直在阅读添加[L,QSA],但这并没有帮助。 对于不使用mod_rewrites的同一台服务器上的简单页面,会话工作正常。

我认为该域是相同的,看它如何从: http:// localhost:8000 / web / someus /login http:// localhost:8000 / web / someus / home

我chmoded&chownedrecursion整个www文件夹,以便Apache拥有所有权限,并拥有网站上的所有内容。

.htaccess文件如下所示:

RewriteCond %{REQUEST_URI} !=/web/[a-z0-9]{6}/index.php RewriteCond %{REQUEST_URI} !error [NC] RewriteCond %{REQUEST_URI} !css [NC] RewriteCond %{REQUEST_URI} !images [NC] RewriteCond %{REQUEST_URI} !js [NC] RewriteRule ^([a-z0-9]{6})/(.*)$ /web/index.php?id=$1&page=$2 [L,QSA] 

httpd.conf有一个DocumentRoot:

 DocumentRoot "/var/www/html" 

httpd.conf有一个别名设置,如下所示:

 Alias /web /var/www/html/website/ <Directory "/var/www/html/website/"> AllowOverride All Order allow, deny Allow from all </Directory> 

在php.ini中打开output_buffering。

 session.cookie_path = /var/www/html/session session.use_cookies = 1 session.use_only_cookies = 1 

标题redirect看起来像包含“home”的$ url值,用url中的homereplacelogin名:

 header("Location: $url",true,302); exit(); 

当我在主页上curl-i时

我得到:

 HTTP/1.1 302 Found Date: Wed, 10 Jun 2015 21:54:38 GMT Server: Apache/2.2.15 (CentOS) X-Powered-By: PHP/5.4.41 Set-Cookie: PHPSESSID=08079c815224b0b129d566dc274e0081; path=/web/someus; domain =127.0.0.1; secure Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: PHPSESSID=ebde43200c30ad6ac18e88b8bfb71371; path=/web/someus; domain =127.0.0.1; secure Set-Cookie: PHPSESSID=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/web/ webdmo; domain=127.0.0.1; secure; httponly Location: login Content-Security-Policy: default-src 'self' 'unsafe-eval' 'unsafe-inline' X-Content-Security-Policy: default-src 'self' 'unsafe-eval' 'unsafe-inline' X-WebKit-CSP: default-src 'self' 'unsafe-eval' 'unsafe-inline' X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block X-Frame-Options: DENY Strict-Transport-Security: max-age=631138519; includeSubDomains Content-Length: 0 Connection: close Content-Type: text/html; charset=UTF-8 

当我在redirect到主页的login页面上curl-i时

我得到:

 HTTP/1.1 200 OK Date: Wed, 10 Jun 2015 21:58:21 GMT Server: Apache/2.2.15 (CentOS) X-Powered-By: PHP/5.4.41 Set-Cookie: PHPSESSID=d79a57eaabb9a41e99f4e0dda202a598; path=/web/someus; domain=127.0.0.1; secure Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-Security-Policy: default-src 'self' 'unsafe-eval' 'unsafe-inline' X-Content-Security-Policy: default-src 'self' 'unsafe-eval' 'unsafe-inline' X-WebKit-CSP: default-src 'self' 'unsafe-eval' 'unsafe-inline' X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block X-Frame-Options: DENY Strict-Transport-Security: max-age=631138519; includeSubDomains Content-Length: 2890 Connection: close Content-Type: text/html; charset=UTF-8 

我认为有趣的是,127.0.0.1域在一个域中是安全的,而不是另一个 – 我不确定这与它有什么关系。

你正在将session.cookie_path设置为/var/www/html/sessionsession.save_path

请参阅上面提供的链接中的定义。 你可能希望session.save_path/var/www/html/session并且保留cookiepath。

会话cookiepath将告诉浏览器这些cookie只能用于您网站上的某些URLpath。

例如,如果我使用/web/someus session.cookie_path来设置一个cookie,然后尝试访问/web/somethingelse ,则以前设置的cookie将不会被发送,因为它不在path/web/someus

如果您将cookiepath保留为默认值,那么会话cookie将在redirect后发送。

弄清楚了。 我认为这是问题的组合。 这两个响应(使用主机本地主机并将cookies.path混合在一起)可能是其中的一部分,以及在redirect时丢失会话的SSLconfiguration错误。 非常感谢你的帮助!