0

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

  1. http://en.example.com to http://en.example.com?lang=en
  2. http://en.example.com/list.php to http://en.example.com/list.php?lang=en
  3. 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.

0

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

In my case, it doesn't need to be generic. I can use the exact country codes (jp|en|kr|cn) and domain (example.com).
It tried your generic rule at htaccess.mwl.be. id=1 is not appended for http://en.example.com/product.php?id=1. The result is http://en.example.com/product.php?lang=en. What I want is like http://en.example.com/product.php?id=1&lang=en
Yes, I only need one rule and it works on the real live server, the online tester htaccess.mwl.be is not just working as expected.
That's right I also tested it on the online tool that doesn't quite work as a real server.
1

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

7 Comments

It almost works. I tried yours at htaccess.mwl.be for http://en.example.com/product.php. The output result is http://en.example.com/product.php?&lang=en with extra &
Because it needs and extra rule to check if query_string is empty.
@Sithu I added an extra rule to verify if query string is empty. Tested!
Thanks, but then what is the difference between the first rule and the third rule?
And without !lang it will cause redirect loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.