双 .htaccess:$_GET 变量为空。我能做些什么?
我目前正在开发一个项目,其中我的 Apache 文档根目录与我的 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时,它是空的
- 当调用像/?foo=bar这样的index.php时,$_GET为空
- 当像/index.php?foo=bar这样调用index.php时$_GET被填充
什么可能导致$_GET变量被清空?< /strong> 任何帮助表示赞赏。
编辑:事情变得更加奇怪:
当我保持两个 .htaccess 文件完好无损并放置一个 index.php (或任何与此相关的 index.* )文件时(无论 documentroot 中的内容是什么,都会解析 /public 目录中的 index.php 文件并正确填充 $_GET 变量。
I am currently working on a project in which my Apache documentroot differs from the public directory in which my index.php resides. Strangely, due to this setup, my $_GET superglobal seems to be emptied.
Directory structure:
- /Documentroot
- /Documentroot/.htaccess
- /public/.htaccess
- /public/index.php
Things go wrong after the first .htaccess is processed.
#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]
Some observations:
- When removing the primary .htaccess and placing an additional index.php in the documentoot, my $_GET variables show fine
- When dumping $_GET in the /public/index.php file, it is empty
- When calling index.php like /?foo=bar, $_GET is empty
- When calling index.php like /index.php?foo=bar $_GET is populated
What could be causing the $_GET variable to be emptied? Any help is appreciated.
EDIT: things just got a whole lot stranger:
When I leave both .htaccess files intact and place an index.php (or any index.* for that matter) file (no matter what content) in the documentroot, the index.php file in the /public directory is parsed and the $_GET variable is properly populated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里猜测,但是当请求
/?foo=bar
时,正则表达式 (.+) 可能会尝试匹配空字符串,从而导致测试失败。尝试将其更改为 (.*)。编辑:实际上,再想一想,当请求
/?foo=bar
时,如果它匹配“/”,那么它会重写为public//
,它将被重写为index.php
按照您的最后一条规则。也许尝试^/?(.*)$
作为您的第一个模式。I'm guessing here, but when requesting
/?foo=bar
, The regex (.+) may be trying to match against the empty string, which fails the test. Try changing it to (.*).Edit: Actually, on second thought, when requesting
/?foo=bar
, if it matches "/", then it rewrites topublic//
, which will get rewritten toindex.php
by your last rule. Maybe try^/?(.*)$
as your first pattern.发生的情况是这样的:
/?foo=bar
documentroot/.htaccess
中的RewriteRule
重定向到public/?foo=bar< /code>
public/.htaccess
中,RewriteCond
检查它是文件、符号链接还是目录。字符串/?foo=bar
不是这些,因此第一个RewriteRule
被跳过。RewriteRule ^.*$ index.php [NC, L]
运行。这会将所有内容定向到index.php
——这是一个没有任何 $_GET 变量的 URL。这就是他们消失的原因。您需要在最后一个
RewriteRule
中传递这些 GET 变量。Here's what's happening:
/?foo=bar
RewriteRule
indocumentroot/.htaccess
redirects topublic/?foo=bar
public/.htaccess
, theRewriteCond
s check whether it is a file, a symlink, or a directory. The string/?foo=bar
is none of those, so the firstRewriteRule
is skipped.RewriteRule ^.*$ index.php [NC, L]
is run. This directs all toindex.php
-- which is a URL without any $_GET variables. That's why they're disappearing.You need to pass those GET variables in that last
RewriteRule
.