【问题标题】:Htaccess 301 redirect one url to another with special characters doesnt workHtaccess 301 使用特殊字符将一个 url 重定向到另一个不起作用
【发布时间】:2014-12-10 20:17:59
【问题描述】:

我需要 301 将一个 URL 重定向到另一个。现在我在.htaccess中有这条规则

RewriteRule /forum/viewtopic.php?f=65&t=6 /page/name-of-the-page [R=301]

RewriteRule /forum/viewtopic.php?f=30&t=5 /page/another-name-of-the-page [R=301]

但它根本不起作用。有人可以帮我吗?

非常感谢您!

【问题讨论】:

  • 您是否能够让其他 RewriteRules 成功触发?您应该考虑为调试目的配置 RewriteLog (httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog)
  • 是的,我有更多的规则,一切正常,但这些简单的行不行。我想,问题出在 ?f=65&t=6 等,因为简单的 RewriteRule /forum/ /page/name-of-the-page [R=301] 有效:(

标签: php .htaccess mod-rewrite redirect http-status-code-301


【解决方案1】:

您无法使用RewriteRule 匹配查询字符串。像这样使用它:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^f=65&t=6$
RewriteRule ^forum/viewtopic\.php$ /page/name-of-the-page? [R=301,L]

RewriteCond %{QUERY_STRING} ^f=30&t=5$
RewriteRule ^forum/viewtopic\.php$ /page/another-name-of-the-page? [R=301,L]

? 最后用于剥离任何现有的查询字符串。

【讨论】:

  • 非常感谢,它不起作用,但我会尝试使用它,也许我会发现
  • 只要确保这些是您的 .htaccess 中的前 2 条规则,并确保 /forum/ 文件夹中没有 .htaccess。
  • 哈,它有效!我在 forum/viewtopic\.php$ 之前用 / 而不是 ^ 尝试了它,所以我有 /forum/viewtopic\.php$,它就像一个魅力。非常感谢!
  • 顺便说一句 ^ 应该做什么?
  • ^ in regex 表示输入的开始。
【解决方案2】:
RewriteRule /forum/viewtopic.php?f=30&t=5 /page/another-name-of-the-page [R=301]
RewriteRule /forum/viewtopic.php?f=65&t=6 /page/name-of-the-page [R=301]

只需颠倒顺序,它应该可以工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2015-03-01
    • 2013-08-04
    • 2016-01-28
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    相关资源
    最近更新 更多