.htaccessのRewriteRuleは文字列の最初のインスタンスと一致しますが、完全な文字列とは一致しません
.htaccessでRewriteRuleを使用して、WordpressWebサイトで301リダイレクトを実行しています。別のルールと同じテキストの続きであるRewriteRuleがある場合、それは常に最初のルールに一致することに気づきました。文字列全体にのみ一致するようにこれを修正するにはどうすればよいですか?
動作する最初のルール: RewriteRule ^/?ulock https://biketoeverything.com/2018/06/18/the-u-lock-to-buy/ [L,R=301,NC]
常に間違った投稿に行く2番目のルール(上記のもの): RewriteRule ^/?ulockbracket https://biketoeverything.com/2018/04/24/attach-any-u-lock-to-your-bike/ [L,R=301,NC]
回答
1 anubhava
次のようなルールがある場合:
RewriteRule ^/?ulock ...
RewriteRule ^/?ulockbracket ...
次に、最初のパターンはで始まるURIと一致し、/ulock
これも一致する/ulockbracket
ため、2番目のルールは実行されません。
解決策は、正規表現アンカーを使用して、正確なマッチングを使用するようにすることです。
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]