【问题标题】:Forward one path to another AND convert spaces to hyphens along the way将一条路径转发到另一条路径并沿途将空格转换为连字符
【发布时间】:2021-04-11 10:36:32
【问题描述】:

试图改变 https://www.example.com/search/label/my%20search%20termhttps://www.example.com/tag/my-search-term

根据下面@anubhava 的回答,选项 2 有效:

RewriteEngine On
RewriteRule ^search/label/(\S+)[-\s]+(\S+)$ /tag/$1-$2 [NE,NC,L,R=301]
RewriteRule ^search/label/(\S+)$ /tag/$1 [NE,NC,L,R=301]
RewriteRule ^(search/label/\S+)\s+(\S+\s.*)$ /$1-$2 [NC,L]

谢谢!

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite friendly-url


    【解决方案1】:

    您可以在站点根目录 .htaccess 中尝试这些递归规则:

    选项 1:

    RewriteEngine On
    
    RewriteRule ^(search/label/\S+)\s+(\S+\s.*)$ /$1-$2 [N,NC,DPI]
    
    RewriteRule ^search/label/(\S+)[-\s]+(\S+)$ /tag/$1-$2 [NE,NC,L,R=301]
    
    RewriteRule ^search/label/(\S+)$ /tag/$1 [NE,NC,L,R=301]
    
    

    选项 2:

    RewriteEngine On
    
    RewriteRule ^search/label/(\S+)[-\s]+(\S+)$ /tag/$1-$2 [NE,NC,L,R=301]
    
    RewriteRule ^search/label/(\S+)$ /tag/$1 [NE,NC,L,R=301]
    
    RewriteRule ^(search/label/\S+)\s+(\S+\s.*)$ /$1-$2 [NC,L]
    

    【讨论】:

      【解决方案2】:

      您能否尝试以下,按照所示示例编写。请确保在测试 URL 之前清除浏览器缓存。

      RewriteEngine ON
      RewriteRule ^search/label/([^\s]*)\s([^\s]*)\s(.*)/?$ tag/$1-$2-$3 [NE,R=301,L]
      


      或在此处使用\S:请确保您一次只输入上述规则或遵循一个规则。

      RewriteEngine ON
      RewriteRule ^search/label/(\S*)\s(\S*)\s(.*)/?$ tag/$1-$2-$3 [NE,R=301,L]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-14
        • 2019-06-11
        • 1970-01-01
        • 2011-06-02
        • 2020-11-02
        • 1970-01-01
        • 2015-08-30
        • 2022-08-17
        相关资源
        最近更新 更多