Apache 使用htaccess URL重写追加查询字符串无效
我有以下Apache重写规则:Apache 使用htaccess URL重写追加查询字符串无效,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我有以下Apache重写规则: RewriteCond %{QUERY_STRING} !lang RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com RewriteRule ^(.*)$ http://%1.example.com/$1&lang=%1 [L, QSA] RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com RewriteRule ^(.*)$ http://
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com
RewriteRule ^(.*)$ http://%1.example.com/$1&lang=%1 [L, QSA]
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com
RewriteRule ^(.*)$ http://%1.example.com/$1?lang=%1 [L, QSA]
我所期望的是
http://en.example.com
至http://en.example.com?lang=en
http://en.example.com/list.php
至http://en.example.com/list.php?lang=en
http://en.example.com/product.php?id=1
至http://en.example.com/product.php?id=1&lang=en
http://en.mobile-wifi_rental.local/product.php&lang=en?id=1
对此,您只需要一条规则,如下所示:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1?lang=%1 [L,QSA]
第一条规则的问题是对查询字符串使用了&
而不是?
通用版本为:
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.[^\.]+\.[^\.]+$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1?lang=%1 [L,QSA]
对此,您只需要一条规则,如下所示:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1?lang=%1 [L,QSA]
第一条规则的问题是对查询字符串使用了&
而不是?
通用版本为:
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.[^\.]+\.[^\.]+$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1?lang=%1 [L,QSA]
我更改了您的规则,
%{QUERY\u STRING}
手动添加:
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com
RewriteRule ^$ http://%1.example.com/?lang=%1 [L,QSA]
#Rule for empty query string
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com
RewriteRule ^(.*) http://%1.example.com/$1?lang=%1 [L]
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com
RewriteRule ^(.*) http://%1.example.com/$1?%{QUERY_STRING}&lang=%1 [L]
使用字符串测试:
http://en.example.com/product.php?id=1
结果是http://en.example.com/product.php?id=1&lang=en
我更改了您的规则,%{QUERY\u STRING}
手动添加:
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com
RewriteRule ^$ http://%1.example.com/?lang=%1 [L,QSA]
#Rule for empty query string
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com
RewriteRule ^(.*) http://%1.example.com/$1?lang=%1 [L]
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^(jp|en|kr|cn)\.example\.com
RewriteRule ^(.*) http://%1.example.com/$1?%{QUERY_STRING}&lang=%1 [L]
使用字符串测试:
http://en.example.com/product.php?id=1
结果是http://en.example.com/product.php?id=1&lang=en
在我的例子中,它不需要是通用的。我可以使用准确的国家代码(jp | en | kr | cn
)和域名(example.com
)。它在上尝试了您的通用规则<代码>id=1未附加到http://en.example.com/product.php?id=1
。结果是http://en.example.com/product.php?lang=en
。我想要的是http://en.example.com/product.php?id=1&lang=en
是的,我只需要一条规则,它可以在真正的live服务器上运行,在线测试仪并没有像预期的那样工作。没错,我也在在线工具上测试了它,但它并不像真正的服务器那样工作。在我的情况下,它不需要是通用的。我可以使用准确的国家代码(jp | en | kr | cn
)和域名(example.com
)。它在上尝试了您的通用规则<代码>id=1未附加到http://en.example.com/product.php?id=1
。结果是http://en.example.com/product.php?lang=en
。我想要的是http://en.example.com/product.php?id=1&lang=en
是的,我只需要一条规则,它可以在真正的live服务器上运行,在线测试仪并没有像预期的那样工作。没错,我也在在线工具上测试了它,但它不能像真正的服务器那样工作。它几乎可以工作。我在为http://en.example.com/product.php
。输出结果为http://en.example.com/product.php?&lang=en
带有额外的&
,因为它需要额外的规则来检查查询字符串是否为空。@Sithu我添加了额外的规则来验证查询字符串是否为空。测试!谢谢,但是第一条规则和第三条规则有什么区别?没有!lang
它将导致重定向循环。它几乎可以工作。我在为http://en.example.com/product.php
。输出结果为http://en.example.com/product.php?&lang=en
带有额外的&
,因为它需要额外的规则来检查查询字符串是否为空。@Sithu我添加了额外的规则来验证查询字符串是否为空。测试!谢谢,但是第一条规则和第三条规则有什么区别?没有!lang
它将导致重定向循环。