Problem with redirecting htaccess 301 with URL variables

If I use this code, it will succeed:

Redirect 301 /products.php http://website.com.au/product_123.php 

But if I do this, it is not:

 Redirect 301 /products.php?id=123 http://website.com.au/product_123.php 

Note that the variable in the url is causing the error.

What am I doing wrong? Is there any other way to do this? I really need URL variables.

+6
source share
2 answers

You cannot put query string parameters in the original URI path of the Redirect directive. To do this, you need the mod_rewrite %{QUERY_STRING} :

 RewriteEngine On RewriteCond %{QUERY_STRING} ^id=123$ RewriteRule ^/?product\.php$ http://website.com.au/product_123.php? [L,R=301] 

Or make it more general:

 RewriteEngine On RewriteCond %{QUERY_STRING} ^id=([^&]+) RewriteRule ^/?product\.php$ http://website.com.au/product_%1.php? [L,R=301] 
+16
source

PHP uses the base64_encode and base64_decode functions. First encode the URI and then decode into your redirect function.

-2
source

All Articles