【问题标题】:Rewrite rule change the URL structure重写规则更改 URL 结构
【发布时间】:2013-05-05 20:20:59
【问题描述】:

我正在尝试将旧页面重定向到更新后的新链接结构页面,因为旧页面现在提供 404

domain.com/artist-some-name

新的

domain.com/artist/some_name.html

在某些名称之间放置 - 和 _ 而不是空格 所以基本上我想用 / 替换 - 分隔符,我可以用

RewriteRule ^artist-(.+)$ /artist/$1.html [R=301,L]

但我不知道如何将 some-name 更改为 some_name,以便正确重定向

编辑 RewriteRule ^artists-(.+)-(.+)$ /artist/$1_$2.html [R=301,L] RewriteRule ^artists-(.+)$ /artist/$1.html [R=301,L]

似乎这对url中是否有空格都有效

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    重写规则 ^artist-(.+)-(.+)$ /artist/$1_$2.html [R=301,L]

    【讨论】:

    • 是的,但有些艺术家只有一个词,因此他们的链接不会有任何 - 或 _ 符号,因为没有空格
    • 我认为需要另一个规则来用_替换每个-
    • 是的,不知道该怎么做
    【解决方案2】:

    试试这些:

    RewriteRule ^artist-([a-z]+)-([a-z]+)$ /artist/$1_$2.html [NC,R=301,L]
    RewriteRule ^artist-([a-z]+)$ /artist/$1.html [NC,R=301,L]
    

    您需要限制通配符,以便只包含字母。如果您需要包含数字(正则表达式中的“数字”),则可以在 a-z 旁边添加 0-9。或者,您可以使用简写为\d+

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 2014-08-10
      • 2020-03-26
      相关资源
      最近更新 更多