Php 强制用户使用带有重定向参数的WWW域
我想重定向这个:Php 强制用户使用带有重定向参数的WWW域,php,apache,.htaccess,mod-rewrite,rewrite,Php,Apache,.htaccess,Mod Rewrite,Rewrite,我想重定向这个: example.com/product-name-p12.html(它的product_page.php?id=12重写) 致: 如果我使用此重写规则: RewriteRule ^(.*)-p(.*).html$ product_page.php?id=$2&%{QUERY_STRING} RewriteCond %{HTTP_HOST} !^www.example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com
example.com/product-name-p12.html
(它的product_page.php?id=12重写)
致:
如果我使用此重写规则:
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]
当然可以:首先,使用查询字符串将html版本重写为php版本。然后遵循www-redirect。因为您在html之前就将其重写为php,所以它仍然存在。如果您只想从example.com/product-name-p12.html重定向到wwww.example.com/product-name-p12.html,只需删除第一条重写规则。否则,您将从html重定向到php,然后再返回html。
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]