Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

apache - htaccess rewrite based on hostname or domain name

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...