【问题标题】:Rewrite URL condition - Remove category in URL and trailing numbers重写 URL 条件 - 删除 URL 中的类别和尾随数字
【发布时间】:2017-08-17 06:27:02
【问题描述】:

在以下条件下将 URL 重写为 301 重定向的最佳方法是什么?

要重写的旧网址示例:
/products/garments/red-yellow-polka-dress-00519
/products/shoes/black-suede-boots-02508

条件:

  1. 将单词 products 更改为 product
  2. 从 URL 中删除类别(例如:/garments/shoes
  3. 从 URL 中删除尾随数字(包括连字符)(例如:-00519-02508

新网址应为:
/product/red-yellow-polka-dress
/product/black-suede-boots

请注意,更改将应用​​于 Wordpress 环境中的 .htaccess 文件。

【问题讨论】:

    标签: regex .htaccess url url-rewriting


    【解决方案1】:

    您可以在RewriteEngine On 行下方设置此规则:

    RewriteEngine On
    
    RewriteRule ^products/[\w-]+/(.+)-\d+/?$ /product/$1 [L,NC,R=301]
    

    【讨论】:

    • 你明白了!你介意一个后续问题吗?这是类别 URL。如果 URL 是 /c/garments/red-yellow-polka-dress-10_450 并且我希望将其重写为 /c/garments/red-yellow-polka-dress 怎么样? (类别保留,尾数有下划线)
    • 然后使用:RewriteRule ^(c/garments/.+)-[\d_]+/?$ /$1 [L,NC,R=301]
    • 谢谢,不过请注意c改成category,而garments是动态词(视实际类别而定)...应该怎么写?
    • c is changed to category 是什么意思?
    • 我的意思是将URL中的/c/改成/category/。如果您有兴趣回答,我为此发布了一个新问题:stackoverflow.com/q/45728769/1399030
    猜你喜欢
    • 2016-08-29
    • 2019-09-10
    • 2014-12-07
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 2015-02-10
    • 2021-06-25
    相关资源
    最近更新 更多