强制用户使用带有重定向参数的WWW域


Force users to use the WWW domain with Redirected parameters

我想重定向这个:

example.com/product-name-p12.html(它是product_page.php?id=12重写的(

至:

www.example.com/product-name-p12.html

如果我使用这个重写规则:

RewriteRule ^(.*)-p(.*).html$ product_page.php?id=$2&%{QUERY_STRING}
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

它正在重定向到www.example.com/product_page.php?id=12,而不是html版本。

非常感谢,

反转规则的顺序并使用QSA标志。QSA(查询字符串附加(标志在添加新查询参数时保留现有查询参数。

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www'.example'.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NE]
RewriteRule ^(.*)-p(.*)'.html$ product_page.php?id=$2 [L,QSA]