Multilanguage subdomain mod_rewrite


I am in the process of setting-up multi language support for my website. Currently everything works when I add the appropriate parameter in the URL. For example: or or

However, instead of constantly adding “?lang=fr” or “?lang=es” , depending on the language, I would like to do it via subdomains.

So, I have created two subdomains on the server which points to
and which points to

When I go to I want the equivalent of going to

So far I have the following in my htaccess for the fr subdomain, which somewhat works:

(this htaccess is located in the “” directory

RewriteEngine on
RewriteRule ^ https:/ [L]

So this redirects “” to “https:/” and so it works partially. I want to keep “” in the browser URL bar, not sure how to do this?

Big problem is that the above doesn’t work for any subpages, such as “

So I tried the following:

RewriteEngine on
RewriteRule ^{REQUEST_URI}&lang=fr [L]

However, neither of these 2 methods keeps “” in the browser bar.

Any help would be much appreciated!

It would be a lot easier to let your software that’s running behind Apache base the language used on the (sub)domain rather than a query parameter.

For instance, if this is PHP, rather than looking at $_GET['lang'] look at $_SERVER['HTTP_HOST'] to find out which language should be used.


$subdomain = explode($_SERVER['HTTP_HOST'], '.')[0];
$language = $subdomain === 'domain' ? 'en' : $subdomain;

This is brilliant - thank you!

To others who may have this issue, I have set the language domains to point to the root (public_html) directory. Then I utilized rpkamp’s code, and everything works perfectly.

1 Like