【问题标题】:Apache mod_rewrite with multiple parameters具有多个参数的 Apache mod_rewrite
【发布时间】:2010-01-05 19:22:00
【问题描述】:

我一直在努力让以下重写工作

^/agatedepot/([0-9.]+)/([0-9a-zA-Z._]+)\?doc-id=([0-9a-zA-Z._\-]+)

/agateDepot.php?date=$1&filename=$2&doc-id=$3

我知道 mod_rewrite 正在工作。我知道它正在读取 .htaccess 文件,我只是没有看到任何重定向发生。这是我的 .htaccess 文件中的内容

RewriteEngine On
RewriteRule ^/agatedepot/([0-9.]+)/([0-9a-zA-Z._]+)\?doc-id=([0-9a-zA-Z._\-]+) /agateDepot.php?date=$1&filename=$2&doc-id=$3

任何帮助将不胜感激。我想这很简单,但我无法弄清楚。 Apache错误日志中没有错误,访问日志只是记录了一个404。

【问题讨论】:

  • 这个问题更适合ServerFault。
  • 你应该给出一个测试的具体 URL 的例子。
  • 确定 /agatedepot/2010.01.03/a_bunch_of_text.txt?doc-id=xt.l.nfl.com.7182032

标签: apache mod-rewrite


【解决方案1】:

URL 查询不是 URL 路径的一部分,因此无法使用 RewriteRule 处理。在这里你需要一个额外的RewriteCond directive。此外,当您在 .htaccess 文件中使用 mod_rewrite 时,会在测试规则之前删除每个目录的路径前缀,并在应用规则后附加回来。因此,在您的情况下,您需要从模式中删除前导 /

RewriteCond %{QUERY_STRING} ^doc-id=([0-9a-zA-Z._\-]+)$
RewriteRule ^agatedepot/([0-9.]+)/([0-9a-zA-Z._]+)$ agateDepot.php?date=$1&filename=$2&doc-id=%1

【讨论】:

  • 完美运行。非常感谢!
  • 如果您只想将原始请求的查询附加到新的查询中,您还可以设置 QSA 标志。
【解决方案2】:

问号? 标志着QueryString 的开始,因此RewriteRule 不对其进行分析。您应该替换? 或用RewriteCond %{QUERY_STRING} ... 分析它。

【讨论】:

    【解决方案3】:

    尝试添加RewriteBase /并跳过第一个斜杠(即^agatedepot...

    【讨论】:

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