Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If I use this code, it's successful:

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

But if I do this, it isn't:

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

Note the variable in the url is what's causing it to fail.

What am I doing wrong? Is there another way to do this? I really need the urls variables.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
509 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can't put query string parameters in the source URI path of the Redirect directive. You'll have to use mod_rewrite's %{QUERY_STRING} variable for that:

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

Or to make it more general:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...