0

我们有一个安装到根目录中的在线购物车 - 然后我们决定要安装一个 CMS ito 根目录并将购物车移动到子域。

所以我们有

  1. 域名.com
  2. shop.domain.com

我们试图实现的是重定向如下 URL:

domain.com/product_info.php?products_id=X

至:

shop.domain.com/product_info.php?products_id=X

X 的值也需要改变的地方。

我读过(如果我理解正确的话)这将与%{REQUEST_FILENAME}此有关,到目前为止,我们在我们.htaccess的 .也。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co.uk$
RewriteRule ^product_info\.php\/?(.*)$ "http\:\/\/sub.domain\.co.uk\/product_info\.php\?products_id\=1$1" [R=301,L]
4

1 回答 1

1

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
于 2013-09-25T14:05:31.837 回答