Home > OS >  How to create a redirect rule for 404 pages with many different path
How to create a redirect rule for 404 pages with many different path

Time:12-21

Here my problem, the site I'm working on has many 404 pages, but they used to be the same pages with a different path

/mens/designers/mens/wales-bonner
/mens/designers/casual-shirts/wales-bonner
/mens/designers/coats-and-jackets/wales-bonner

etc.

THe client wants the redirect to go to the category, so

/mens/designers/mens/
/mens/designers/casual-shirts/
/mens/designers/coats-and-jackets/

I'm pretty sure, there must be a way to have regex rule to cover them all, but I can't seem to find how

Something like RewriteRule ^/mens/designers/(.*)/wales-bonner /mens/designers/(.*)

but it doesn't work, I don't know how to group the middle part of the URL

Can anyone help ?

CodePudding user response:

I see several potential problems with your rewrite rule:

  1. You have a capturing group in the output URL rather than using $1 to use the result of the capturing group from the input URL.
  2. ^/ won't work in .htaccess, only in Apache .conf files. I' would use ^/? instead which will work in either context. That makes the starting slash optional.
  3. You don't include an [R] flag on the rule to make it a redirect.
  4. You don't include an [L] flag on the rule to prevent any following rules from interfering with it.

You can also add "mens/designers" to the capturing group so that you don't have to repeat it in the output. I would suggest:

RewriteRule ^/?(mens/designers/.*)/wales-bonner /$1 [R=301,L]
  • Related