该QSA
标志将自动将查询字符串传回,将其.htaccess
放在域的根文件夹中:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php [R=301,QSA,L]
如果子域也在您域的同一根文件夹中,则使用以下命令:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php [R=301,QSA,L]
所以基本上符合上述任何规则,如果用户访问:
domain.com/product_info.php?products_id=4
domain.com/product_info.php?products_id=3
domain.com/product_info.php?products_id=2
domain.com/product_info.php?products_id=1
它将被重定向到:
shop.domain.com/product_info.php?products_id=4
shop.domain.com/product_info.php?products_id=3
shop.domain.com/product_info.php?products_id=2
shop.domain.com/product_info.php?products_id=1
如果您实际上需要更改 ID,您可以这样做:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} products_id=([^&]+) [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php?products_id=1%1 [R=301,L]
如果域和子域位于同一个根文件夹中,则如下所示:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteCond %{QUERY_STRING} products_id=([^&]+) [NC]
RewriteRule ^product_info\.php$ http://shop.domain.co.uk/product_info.php?products_id=1%1 [R=301,L]
基本上你需要使用%{QUERY_STRING}
从查询字符串中获取数据。
因此,使用上述 2 条规则中的任何一条,如果用户访问:
domain.com/product_info.php?products_id=4
domain.com/product_info.php?products_id=3
domain.com/product_info.php?products_id=2
domain.com/product_info.php?products_id=1
它将被重定向到:
shop.domain.com/product_info.php?products_id=14
shop.domain.com/product_info.php?products_id=13
shop.domain.com/product_info.php?products_id=12
shop.domain.com/product_info.php?products_id=11