How can I change the value of a query string parameter with a redirect?
I want to redirect "http://w开发者_Python百科ww.suma.ir/product.php?id_product=12" to "http://www.suma.ir/product.php?id_product=508" but I'm having trouble. The URL path should stay the same, all I want to do is change the ID in the query string. What do I need to do to make this work?
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^suma\.ir$ [NC]
RewriteRule ^(.*)$ http://www.suma.ir/$1 [R=301,L]
# This is the part that isn't working
Redirect 301 /product.php?id_product=12 http://www.suma.ir/product.php?id_product=508
The mod_alias Redirect directive doesn't look at the parameter string, so your Redirect statement will never match. Instead, you'll need to use mod_rewrite. You can do something like the following:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^suma\.ir$ [NC]
RewriteRule ^(.*)$ http://www.suma.ir/$1 [R=301,L]
RewriteCond %{QUERY_STRING} (^|&)id_product=12(&|$)
RewriteRule ^product\.php$ /$0?id_product=508 [R=301,L]
精彩评论