Reputation: 4862
I have this Apache Rewrite rules:
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]
What I expected is
http://en.example.com
to http://en.example.com?lang=en
http://en.example.com/list.php
to http://en.example.com/list.php?lang=en
http://en.example.com/product.php?id=1
to http://en.example.com/product.php?id=1&lang=en
(1) and (2) is fine, but what I got for (3) is
http://en.mobile-wifi_rental.local/product.php&lang=en?id=1
.
Upvotes: 0
Views: 218
Reputation: 4284
I changed your rules, %{QUERY_STRING}
added manually:
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]
Tested with string: http://en.example.com/product.php?id=1
and the result is http://en.example.com/product.php?id=1&lang=en
Upvotes: 1
Reputation: 5748
You only need one rule for this, here it is:
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]
The problem with your first rule was the use of &
instead of ?
for the query string.
A generic version of it would be:
RewriteCond %{QUERY_STRING} !lang
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.[^\.]+\.[^\.]+$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1?lang=%1 [L,QSA]
Upvotes: 1