Scott Wilson Scott Wilson - 3 months ago 12
Apache Configuration Question

.htaccess redirect to change parameter value

I would like to change the URL

/index.php?main_page=product_info&cPath=1&products_id=4


to

/index.php?main_page=product_info&cPath=141&products_id=302


I have tried

RewriteRule ^products_id=4$ products_id=302


but that doesn't seem to match.

My
.htaccess
file currently contains

RewriteEngine on
Options +FollowSymLinks
RewriteRule ^products_id=302$ products_id=4 [L,R=301]


It would seem like I could use something like

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)products_id=4(&.*)?$
RewriteRule ^index\.php$ /index.php?%1products_id=302%3 [L,R=301]


but that doesn't work reliably. (If I click on a link, it doesn't work, but if I enter that URL in the address bar, it does.)

Answer

Try the following rule :

RewriteEngine on

RewriteCond %{THE_REQUEST} /index\.php\?main_page=([^&]+)&cPath=1&products_id=4\sHTTP [NC]
RewriteRule ^ /index.php?main_page=%1&cPath=141&products_id=302 [L,R]