【问题标题】:Dual .htaccess: $_GET variable empties. What can I do?双 .htaccess:$_GET 变量清空。我能做些什么?
【发布时间】:2011-05-04 14:19:01
【问题描述】:

我目前正在处理一个项目,其中我的 Apache 文档根目录与我的 index.php 所在的公共目录不同。奇怪的是,由于这个设置,我的 $_GET superglobal 似乎被清空了。

目录结构:

  • /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 变量被清空?感谢任何帮助。

编辑:事情变得非常陌生:

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

【问题讨论】:

  • 可能不是问题,但您确定文档根目录中没有index.htm 或其他类似文件吗?尝试将DirectoryIndex index.php 添加到您的.htaccess
  • 感谢您的评论,但我确信这不是问题。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

这是发生了什么:

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

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

【讨论】:

  • 我实际上已经尝试将 QSA 标志添加到 rewriterule,但这不起作用。这不是问题。
【解决方案2】:

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

编辑:实际上,再想一想,当请求/?foo=bar 时,如果它匹配“/”,那么它会重写为public//,根据您的最后一条规则,它将被重写为index.php。也许尝试^/?(.*)$ 作为你的第一个模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多