RewriteCond和rewriteRule根据域进行重定向 [英] RewriteCond and rewriteRule to redirect depending on the domain
问题描述
首先,我有几个域指向同一网页,每个域对应一种不同的语言.网页(Drupal)使用URL(example.com/en
)中的/lang
参数标识语言.
我需要将每个域重定向到其对应的语言,所以我需要类似以下内容:
First of all, I've got a few domains pointing to the same webpage, each domain corresponfing to a different language. The webpage (Drupal) identifies the language using a /lang
parameter in the url (example.com/en
).
I need to redirect every domain to its corresponding language so I need something like:
- example.com-> example.com/en
- example.ru-> example.ru/ru
- example.fr-> example.fr/fr
我在htaccess中定义了一些规则,但它们没有达到我的期望:
I defined some rules in htaccess but they don't do what i expected:
# Rewrite --- http://www.example.com => http://www.example.com/en
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^$ /en? [L,R=301]
# Rewrite --- http://www.example.ru => http://www.example.ru/ru
RewriteCond %{HTTP_HOST} !^www\.example\.ru [NC]
RewriteRule ^$ /ru? [L,R=301]
不是将example.com
更改为example.com/en
,将example.ru
更改为example.ru/ru
,而是将/en
附加到所有域.
是我想念的东西吗?
Instead of changing example.com
to example.com/en
and example.ru
to example.ru/ru
it appends /en
to all domains.
Is it something I am missing?
任何建议都会很有帮助.
Any advice would be very helpful.
推荐答案
这应该有效:
# Rewrite --- http://www.example.com => http://www.example.com/en
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/en(/(.*)$|$)
RewriteRule ^ /en%{REQUEST_URI} [L,R=301]
# Rewrite --- http://www.example.ru => http://www.example.ru/ru
RewriteCond %{HTTP_HOST} ^www\.example\.ru [NC]
RewriteCond %{REQUEST_URI} !^/ru(/(.*)$|$)
RewriteRule ^ /ru%{REQUEST_URI} [L,R=301]
如果要使重写对用户不可见,可以删除,R=301
.
You can remove ,R=301
if you want to make the rewrite invisible to the user.
这篇关于RewriteCond和rewriteRule根据域进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!