htaccess rewrite to remove query string

2019-08-22 03:38发布

问题:

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]