【问题标题】:RewriteRule for 301 redirect301 重定向的 RewriteRule
【发布时间】:2011-12-10 17:49:15
【问题描述】:

我浏览了所有关于重写规则问题的回答线程。尝试了这些建议,但我仍然不知道我做错了什么。

我想对我们的旧 URL 进行 301 重定向到新 URL。示例:

old url : http://www.xyz.com/abc/topics.html
new url : http://www.xyz.com/index.php#first

我正在 .htaccess 中尝试遵循规则:

RewriteEngine on
RewriteRule ^\/abc\/(.+)$ http://www.xyz.com/index.php#first [L,R=301]

非常感谢任何建议

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    试试这个:

    RewriteRule ^abc\/(.+)$ http://www.example.com/index.php#first [L,R=301,NE]
    

    【讨论】:

    • 谢谢,此解决方案重定向到目标页面,但在 url 而不是 '#' 中显示的是 '%23',因此不会滚动到锚点。
    • 找到了解决方案,也必须使用 [NE] 标志。所以现在是:^abc\/(.+)$ xyz.com/index.php#first [L,R=301,NE]
    • 详细信息:NE 标志防止特殊字符(例如#、?、&)被转换为其十六进制代码。 L 阻止执行进一步的规则,从而阻止可能的“无效请求”警告。见:httpd.apache.org/docs/2.4/rewrite/flags.html
    【解决方案2】:

    当然,您可以通过“mod_rewrite”来完成,但在这种情况下,我建议您使用mod_alias,因为它更快更简单(请参阅此 SO 回复:mod_rewrite or mod_alias?

    像这样:

    Redirect permanent /abc http://www.xyz.com/index.php#first
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多