【问题标题】:RewriteRule in .htaccess matches the first instance of string but not the complete string.htaccess 中的 RewriteRule 匹配字符串的第一个实例,但不匹配完整的字符串
【发布时间】:2020-06-12 19:06:08
【问题描述】:

我在我的 .htaccess 中使用 RewriteRule 在我的 Wordpress 网站上执行一些 301 重定向。我注意到,如果我有一个 RewriteRule 是与另一个规则相同的文本的延续,它总是匹配第一个规则。我该如何解决这个问题,使它只匹配完整的字符串?

第一个有效的规则:RewriteRule ^/?ulock https://biketoeverything.com/2018/06/18/the-u-lock-to-buy/ [L,R=301,NC]

总是发错帖子的第二条规则(上一条):RewriteRule ^/?ulockbracket https://biketoeverything.com/2018/04/24/attach-any-u-lock-to-your-bike/ [L,R=301,NC]

【问题讨论】:

  • 确保使用锚点,例如^/?ulock/?$^/? ulockbracket/?$
  • 看起来有效!随意将其发布为答案,以便我可以给您接受的答案(如果您认为我的问题可能对其他人有用,请支持我的问题)

标签: wordpress .htaccess redirect mod-rewrite


【解决方案1】:

当你有规则时:

RewriteRule ^/?ulock ...
RewriteRule ^/?ulockbracket ...

然后第一个模式匹配任何以 /ulock 开头的 URI,它也将匹配 /ulockbracket,因此第二个规则不会触发。

解决方案是确保使用正则表达式锚以确保我们使用精确匹配:

RewriteRule ^/?ulock/?$ https://biketoeverything.com/2018/06/18/the-u-lock-to-buy/ [L,R=301,NC]
RewriteRule ^/?ulockbracket/?$ https://biketoeverything.com/2018/04/24/attach-any-u-lock-to-your-bike/ [L,R=301,NC]

在较短的匹配之前使用较长的匹配模式

RewriteRule ^/?ulockbracket https://biketoeverything.com/2018/04/24/attach-any-u-lock-to-your-bike/ [L,R=301,NC]
RewriteRule ^/?ulock https://biketoeverything.com/2018/06/18/the-u-lock-to-buy/ [L,R=301,NC]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    相关资源
    最近更新 更多