【问题标题】:htaccess - Rewrite two random segments with one statichtaccess - 用一个静态重写两个随机段
【发布时间】:2018-11-04 18:25:28
【问题描述】:

您好,我尝试通过旧路径中的 htacces 重写 URL

https://example.com/staticword-randomword/random-words/target.php

到新的路径

https://example.com/new/target.php

我尝试用这个重写命令来管理这个:

RewriteRule ^staticword(.*)/(.*)/ new/$1 [L,R=301]

不幸的是,我对这些重写命令和重写的 URL 不是很好

https://example.com/new/-randomword/random-words/

有人可以指出解决方案吗?

提前致谢。

【问题讨论】:

    标签: apache .htaccess url url-rewriting


    【解决方案1】:

    您的正则表达式不正确,.* 本质上是贪婪的。

    您可以使用此重定向规则:

    RewriteEngine On
    
    RewriteRule ^staticword-[^/]*/[^/]*/([^/]+)/?$ /new/$1 [L,R=301,NC,NE]
    

    确保使用新浏览器测试此更改以避免旧浏览器缓存。

    【讨论】:

    • 非常感谢。对不起,我的正则表达式技能不好,这对我来说是一本密封的书。您的重写规则效果很好,除了 NC 和 NE 之间的拼写错误,用逗号替换它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2018-06-27
    • 2017-10-01
    相关资源
    最近更新 更多