【问题标题】:Why does the htaccess rewritecond using %{query_string} only work when the URL contains index.php?为什么使用 %{query_string} 的 htaccess rewritecond 仅在 URL 包含 index.php 时才有效?
【发布时间】:2014-10-26 14:25:38
【问题描述】:

我有一个像这样的 htaccess 重定向:

RewriteCond %{QUERY_STRING} tabid=53
RewriteRule . http://www.example.com/foobar? [R=301,L]

所以,当我访问 example.com/?tabid=53 时,没有任何反应,example.com/index.php?tabid=53 被重定向到 /foobar。当我添加另一个条件时:

RewriteCond %{REQUEST_URI} ^(.*)$

什么都不会改变,根据我的理解,这应该说“uri 中是否有 index.php 无关紧要”。我做错了什么?

【问题讨论】:

标签: .htaccess mod-rewrite


【解决方案1】:

你试过了吗:

RewriteCond %{QUERY_STRING} tabid=53
RewriteRule ^(.*)$ http://www.example.com/foobar? [R=301,L]

我认为您的单点的问题是,该点与任何单个字符匹配,但在您的 example.com/?tabid=53 中没有可匹配的字符。如果你使用example.com/i?tabid=53,那么它可以工作。

【讨论】:

  • 提供一些描述
  • 其实是因为RewriteRule . , RewriteRule ^ 作品:)
【解决方案2】:

您可以通过匹配 .* 而不是 . 来避免这种奇怪的行为。
您还可以匹配root levelindex.php

两种解决方案都按预期工作。

解决方案 1

RewriteEngine On

RewriteCond %{QUERY_STRING} tabid=53 [NC]
RewriteRule .* /foobar? [R=301,L]

解决方案 2

RewriteEngine On

RewriteCond %{QUERY_STRING} tabid=53 [NC]
RewriteRule ^(|index\.php)$ /foobar? [R=301,L]

结论

你的规则有问题

RewriteRule . http://www.example.com/foobar? [R=301,L]

是因为. 表示one character 并且永远不会匹配,因为它是空的(没有文件,直接在根级别查询字符串)。

这就是为什么.* 匹配(表示0 or more characters)或RewriteRule ^(|index\.php)$(表示match root level -empty- or index.php

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 2011-10-13
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    相关资源
    最近更新 更多