。htaccess中的RewriteRule匹配字符串的第一个实例,但不匹配完整的字符串

问题描述 投票:0回答:1

我正在.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]

wordpress .htaccess redirect mod-rewrite
1个回答
0
投票

当您具有以下规则时:

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

然后,第一个模式匹配以/ulock开头的任何URI,该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]
© www.soinside.com 2019 - 2024. All rights reserved.