.htaccess使用查询字符串重写URL不同的页面

时间:2013-12-18 11:01:28

标签: php regex apache .htaccess mod-rewrite

我有一个旧页面,用于根据查询字符串显示多个产品。我想用查询字符串将URL重写为与旧url将被重写的静态页面不同的静态页面。

   RewriteRule ^old.php$ new.php

   RewriteRule old.php?product=10 different.php [R=301,L]

在这种情况下,old.php会被重写为new.php。但是old.php?product = 1O被重写为 new.php?product = 10 而不是不同.php

1 个答案:

答案 0 :(得分:2)

您无法在重写规则中匹配QUERY_STRING。您需要使用RewriteCond %{QUERY_STRING}来匹配查询字符串。

将您的规则用作:

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^old\.php$ new.php [L,R]

RewriteCond %{QUERY_STRING} ^product=10$
RewriteRule ^old\.php different.php [L,R]