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

发布于 2024-09-30 07:54:16 字数 1184 浏览 1 评论 0原文

我目前正在开发一个项目,其中我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一张白纸 2024-10-07 07:54:16

我在这里猜测,但是当请求 /?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 to public//, which will get rewritten to index.php by your last rule. Maybe try ^/?(.*)$ as your first pattern.

合约呢 2024-10-07 07:54:16

发生的情况是这样的:

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

您需要在最后一个 RewriteRule 中传递这些 GET 变量。

Here's what's happening:

  1. You request /?foo=bar
  2. RewriteRule in documentroot/.htaccess redirects to public/?foo=bar
  3. In public/.htaccess, the RewriteConds check whether it is a file, a symlink, or a directory. The string /?foo=bar is none of those, so the first RewriteRule is skipped.
  4. RewriteRule ^.*$ index.php [NC, L] is run. This directs all to index.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文