根据urlpath更改域

我有一个多语言的网站,在不同的领域,在语言上的依赖。 所有域共享相同的字体代码,网站(Drupal)使用该path确定语言。 现在我已经configuration每个域redirect到自己的语言,所以我有:

  • www.example.com – > www.example.com/en
  • www.example.es – > www.example.es/es
  • www.example.ru – > www.example.ru/ru

这样,当用户进入网站时,cms会将语言切换到与该域对应的语言。

我的问题是,当有人在网站上使用语言切换器时,它会更改所有查询参数以显示与该语言相对应的内容, 不会更改域名,如果我在www.example.com/en/something并切换到西class牙语,我将结束在www.example.com/es/algo

我还需要根据语言(在域名后面总是写成/en/es/ru )来更改域,如果可能的话,保留查询参数。

我已经写在htaccess这个代码(仅作为testing俄罗斯,但我担心这可能是完全错误的,因为它什么都不做:

  RewriteCond %{REQUEST_URI} ^/ru$1 RewriteRule ^$1 http://www.example.ru/ru$1 [L,R=301] 

任何build议将非常有帮助。

把这些放在你以前的规则之前:

 RewriteCond %{HTTP_HOST} !^www\.example\.com$ RewriteRule ^en/(.*)$ http://www.example.com%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} !^www\.example\.ru$ RewriteRule ^ru/(.*)$ http://www.example.ru%{REQUEST_URI} [L,R=301] ... 

请注意,通过这种方式,例如,如果用户输入www.example.ru/en/something ,则/en in路径将比主机名中的.ru更重要,从而导致重定向到www.example.com/en/something

mod_rewrite使用正则表达式。 $1表示需要在匹配部分“定义”的组(使用括号和.作为“全部匹配”运算符):

 RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] RewriteRule ^ru(.*)$ http://www.example.ru/$1 [L,R=301]