问题
I know using the power of regex, I can reduce my 301's into a few lines. However, I do not have the brain power for it.
Basically our links went from:
http://www.site.com/collection/womens-active (this can also have ?brand=123 queries attached to the end)
To:
http://www.site.com/collection/active
"womens-" is the changed part.
A simple 301 works perfectly, even with queries, but there is about 100 lines for this in htaccess.
With Regex, can we reduce this to a few lines?
回答1:
Use a RedirectMatch
instead:
RedirectMatch 301 ^/collection/([^/-]+)-active/(.*)$ /collection/active/$2
Assuming by the "is the changed part", you mean there could be anything where it says "womens-".
EDIT: Since it's the other way around, just swap the regex grouping and the "womens-":
RedirectMatch 301 ^/collection/womens-([^/-]+)/(.*)$ /collection/$1/$2
来源:https://stackoverflow.com/questions/17820228/301-redirect-rewrites-with-patterns