双.htaccess:$ _GET变量清空。我能做什么?

时间:2010-11-03 16:10:24

标签: php apache .htaccess mod-rewrite

我目前正在开发一个项目,其中我的Apache documentroot与index.php所在的公共目录不同。奇怪的是,由于这种设置,我的$ _GET超全局似乎被清空了。

目录结构:

  • / DocumentRoot的
  • /Documentroot/.htaccess
    • /public/.htaccess
    • /public/index.php

处理完第一个.htaccess后出现问题。

#First .htaccess in Apache documentroot:
RewriteEngine On
RewriteRule (.+) public/$1 [NC,L]

# Second .htaccess in /documentroot/public:
SetEnv APPLICATION_ENV testing
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

一些观察结果:

  • 删除主.htaccess并在documentoot中放置一个额外的index.php时,我的$ _GET变量显示正常
  • 在/public/index.php文件中转储$ _GET时,它是空的
  • 当调用index.php时,如/?foo = bar,$ _GET为空
  • 调用index.php时,如/index.php?foo=bar $ _GET已填充

可能导致$ _GET变量被清空的原因是什么?感谢任何帮助。

编辑:事情变得非常奇怪:

当我将.htaccess文件保留原样并在documentroot中放置index.php(或任何索引。*)文件(无论内容是什么)时,/ public目录中的index.php文件被解析并且正确填充了$ _GET变量。

2 个答案:

答案 0 :(得分:0)

我在这里猜测,但是在请求/?foo=bar时,正则表达式(。+)可能正在尝试匹配空字符串,这会导致测试失败。尝试将其更改为(。*)。

编辑:实际上,在第二次考虑时,在请求/?foo=bar时,如果它与“/”匹配,则会重写为public//,这将被最后一条规则重写为index.php 。也许尝试^/?(.*)$作为您的第一个模式。

答案 1 :(得分:0)

以下是发生的事情:

  1. 您请求/?foo=bar
  2. {li} RewriteRuledocumentroot/.htaccess重定向到public/?foo=bar
  3. public/.htaccess中,RewriteCond检查它是文件,符号链接还是目录。字符串/?foo=bar不是那些,因此跳过第一个RewriteRule
  4. RewriteRule ^.*$ index.php [NC, L]正在运行。这会将所有内容定向到index.php - 这是一个没有任何$ _GET变量的URL。这就是他们消失的原因。
  5. 您需要在最后RewriteRule

    中传递这些GET变量
相关问题