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


.htaccess Rewrite URL different pages with query string

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

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

在这种情况下,旧的.php被重写为新的.php。但是旧的.php?产品=1O被重写为新的.php?产品=10而不是不同的.php

重写规则中不能匹配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]