【问题标题】:How to remove a number from the end of an URL using .htaccess?如何使用 .htaccess 从 URL 末尾删除数字?
【发布时间】:2018-06-07 09:41:58
【问题描述】:

我有很多以 -xxx 结尾的 URL,其中 xxx 是 000 到 999 之间的数字。 例如:

http://a.com/imagea-000
http://a.com/category1/imageb-002
http://a.com/category1/category2/imagec-999

如何在 .httaccess 中指定 RewriteCond/RewriteRule 以删除数字,包括 url 末尾的“-”?所以最终的 URLS 看起来像:

http://a.com/imagea
http://a.com/category1/imageb
http://a.com/category1/category2/imagec

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    RewriteEngine On
    
    RewriteRule ^(.+?)-\d+/?$ /$1 [L,R=302]
    
    • -\d+/?$ 将匹配连字符后跟 1 或数字以及 URI 末尾的可选斜杠。
    • (.+?) 将匹配上述模式之前的任何内容并将其捕获为组 #1
    • 一旦您确认它工作正常,请将R=302 替换为R=301。在测试您的 mod_rewrite 规则时避免使用 R=301(永久重定向)。

    【讨论】:

    • 太棒了!完美运行。非常感谢!
    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 2012-12-13
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    相关资源
    最近更新 更多