I'm a noob when it comes to regex. What I'm trying to accomplish is:
https://www.example.com/shop/product-floating-front-rotor-kit/
should redirect to
https://www.example.com/shop/product-matching-front-rotor/
product should be the name of the product, I have to do this for multiple products.
Edit: This is what I have so far, am I even close?
RewriteEngine On
RewriteRule ^/shop/([a-z] )-floating-front-rotor-kit/ ^/shop/$1-matching-front-rotor/
CodePudding user response:
RewriteRule ^/shop/([a-z] )-floating-front-rotor-kit/ ^/shop/$1-matching-front-rotor/
This is close, except that...
In
.htaccess
the URL-path matched by theRewriteRule
pattern (first argument) does not start with a slash.The substitution string has an erroneous
^
prefix. This should be an "ordinary" string, not a regex.[a-z]
does not match hyphens/dashes, which you state could occur in a product name.You have not included an end-of-string anchor (
$
) on the end of theRewriteRule
pattern, so any trailing string will be successful and discarded. (Is that the intention?)This is an internal "rewrite", not a "redirect" as stated. You need to include the
R
flag. (An internal rewrite is unlikely to work here, since the target URL requires further rewriting.)
Try the following instead. This should go at the top of the .htaccess
file, immediately after the RewriteEngine
directive.
RewriteRule ^shop/([a-z-] )-floating-front-rotor-kit/$ /shop/$1-matching-front-rotor/ [R=302,L]
This is a 302 (temporary) redirect.