I believe there should be a simpler method to rewriting URLs than I currently have and wonder if anyone can help.
The site I am working on has multiple brands for example:
https://example.com/anything/brand/nike/something
https://example.com/anything/brand/puma/something
My current redirect would be
RedirectMatch 301 "(.*)/brand/nike(.*)$" "$1/manufacturer--nike"
To get me the following output, removing /brand/ and replacing nike with manufacturer--nike and finally removing anything that follows i.e. /something.
https://example.com/anything/manufacturer--nike
Now I could add a second rule for Puma and each of the other brands, but I imagine there is a way to match against a list of brands and use one rule but my Google skills have failed me in finding a solution.
Is there a way?
CodePudding user response:
If the number of brands are limited then you could use alternation (eg. nike|puma|brand etc.) in the regex, to match nike or puma or brand.
Aside: To match nike/<something>, you should at least check for that slash, otherwise it's going to match nikeeeee and nikey etc. (Although not necessarily an issue.)
For example:
RedirectMatch 301 "(.*)/brand/(nike|puma|brand)/" "$1/manufacturer--$2"
The (.*)$ at the end of the regex - to simply remove the trailing part of the URL-path - is not required.
The $2 backreference contains the "brand-name" matched by the second capturing (alternation) subpattern.
method to rewriting URLs
Just to clarify, this is an external redirect, it's not "rewriting URLs".
