301 Redirect/Rewrites with Patterns

强颜欢笑 提交于 2019-12-12 02:48:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!