18

I have two different domains (let's say www.site1.com and www.site2.com) that point to the same hosting server.

I need the two different domain names because I want to use the first one for the italian contents and the second one for the english contents. The contents are the same, unless for the language, but the domains have to be different.

So, I'd like to write a rule that lets me translate from:

  • www.site1.com to /?lang=it

  • www.site2.com to /?lang=en

I usually use the same domain name for many different languages rewriting from www.site.com/it/ to /?lang=it (of course, a transparent rewriting - the user doesn't see any different URL).

I'd like to achieve the same using different domains but I can't figure out how... I've been working on it for hours and I can't achieve what I want!

Usually I use this:

RewriteCond %{REQUEST_URI} /([a-z]{2})
RewriteRule ^([a-z]{2})[/]*$ /index.php?lang=$1 [NC,QSA]

I can't get this one work, to use different domains:

RewriteCond %{HTTP_HOST} ^www.site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/index.php?lang=it
RewriteRule ^(.*)$ /index.php?lang=it [NC,QSA]

RewriteCond %{HTTP_HOST} ^www.site2\.com [NC]
RewriteCond %{REQUEST_URI} !^/index.php?lang=en
RewriteRule ^(.*)$ /index.php?lang=en [NC,QSA]
nikoshr
  • 32,926
  • 33
  • 91
  • 105
tobia.zanarella
  • 1,246
  • 1
  • 17
  • 25
  • 3
    possible duplicate of [.htaccess RewriteRule: two domains using same server and directory](http://stackoverflow.com/questions/6972413/htaccess-rewriterule-two-domains-using-same-server-and-directory) – Lawrence Cherone Apr 19 '12 at 16:48
  • What happens with this setup? And if you remove the second RewriteCond (`^/index.php?lang=it` should never match, REQUEST_URI does not expose the query parameters, check %{QUERY_STRING} instead) ? – nikoshr Apr 19 '12 at 17:58
  • I have tried many many different "versions" of the rules in the latter grey block... I always get an HTTP 500 error or just an empty $_GET. I mean, among the others tentatives, with or without the second condition "RewriteCond %{REQUEST_URI} !^/index.php?lang=it". – tobia.zanarella Apr 20 '12 at 06:57
  • Thank you Lawrence Cherone! That one works like a charm! – tobia.zanarella Apr 20 '12 at 07:07
  • 1
    One should not forget the `RewriteEngine on` above the other rules...;) – mtness Apr 05 '18 at 19:38

1 Answers1

34

Lawrence Cherone - Thank you, that one works like a charm! Now it works:

RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC] 
RewriteRule ^(.*)$ index.php?lang=it [NC,QSA] 
RewriteCond %{HTTP_HOST} ^www\.site2\.com [NC] 
RewriteRule ^(.*)$ index.php?lang=en [NC,QSA] 

Of course I check the www redirect before this rule.

Thank you!!

tobia.zanarella
  • 1,246
  • 1
  • 17
  • 25