How to change the value of a query string parameter using redirection?

I want to redirect "http://www.suma.ir/product.php?id_product=12" to "http://www.suma.ir/product.php?id_product=508", but I am having problems. The URL path should remain unchanged, all I want to do is change the identifier 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
+3
source share
1 answer

The mod_alias Redirect directive does not look at the parameter string, so the Redirect statement will never match. Instead, you will 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]
+1
source

Source: https://habr.com/ru/post/1790639/


All Articles