【问题标题】:Why does this pattern not match QUERY_STRING in RewriteCond while it matches in preg_match?为什么此模式与 RewriteCond 中的 QUERY_STRING 不匹配,而在 preg_match 中匹配?
【发布时间】:2012-01-16 02:32:18
【问题描述】:

这是我的 .htaccess 文件中的内容。

RewriteEngine on

RewriteCond %{QUERY_STRING}  ^(run=[a-z0-9A-z]{13})$
RewriteRule %{QUERY_STRING}  \? [L]

我所做的只是为所有QUERY_STRING 重写QUERY_STRING,类似于run=4f13665700694,并且什么也不做。 URL 示例:http://thinkingmonkey.me/runs/?run=4f13665700694。所以, RewriteCond 应该匹配。但是上面的不行。

但是,模式似乎是正确的。因为,preg_matchRewriteRule 都使用 PCRE - Perl Compatible Regular Expressions

我用preg_match试了一下。

$subject = "run=4f13665700694"; 
$pattern = "/^(run=[a-z0-9A-z]{13})$/";

echo preg_match($pattern, $subject);

除此之外,以上将输出:

1

即一场成功的比赛。

我不明白为什么。我在这里错过了什么?

【问题讨论】:

  • “什么都不做”是指在这种情况下重写为http://thinkingmonkey.me/runs/?
  • @TerryE 是的。但是,我在这里解决了问题。 %{QUERY_STRING}不能作为RewriteRule中要匹配替换的模式。

标签: apache .htaccess mod-rewrite apache2 preg-match


【解决方案1】:

我认为这与第二行有关。尝试将其更改为:

RewriteRule ^(.*) $1?

【讨论】:

  • +1 您不能在 RewriteRule 模式中使用变量。编写此正则表达式的更简单方法是 ^(.*),因为查询字符串已经从主题中剥离。
  • 你忘了尾随?在替换字符串上;-)
猜你喜欢
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多