htaccess rewrite to remove query string

给你一囗甜甜゛ 提交于 2019-12-12 01:20:52

问题


I need to rewrite the following:

 http://www.mystuff.com/drinks/category/beer?page=1  

to

 https://www.mystuff.com/food-drink/beer/ale  

No matter what I try the URI rewrite to the new address but it keeps the query string attached. I need to lose this. I've tried so many options and none seem to work, can anybody ofgfer some advice. I thought this would do it, but no:

RewriteCond %{QUERY_STRING}  (.*)(?:^|&)page=(?:[^&]*)((?:&|$).*)
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(/drinks/category/beer)$ https://www.mystuff.com/food-drink/beer/ale  [R=301, L]

Can anybody help?


回答1:


You need to add an empty query string to truncate it on the rewrite. Add a single ? to the end of the rewrite:

RewriteCond %{QUERY_STRING}  (.*)(?:^|&)page=(?:[^&]*)((?:&|$).*)
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(/drinks/category/beer)$ https://www.mystuff.com/food-drink/beer/ale?  [R=301, L]


来源:https://stackoverflow.com/questions/30591437/htaccess-rewrite-to-remove-query-string

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