多语言网站,基于cookie的重定向或接受语言(重写规则) [英] Multilanguage site, redirect based on cookie or accept-language (RewriteRule)

查看:152
本文介绍了多语言网站,基于cookie的重定向或接受语言(重写规则)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个星期前,我问一个关于多语言网站的问题(见<一href="http://stackoverflow.com/questions/3439668">http://stackoverflow.com/questions/3439668).

A few weeks ago i asked a question about a multilanguage site (see http://stackoverflow.com/questions/3439668).

这完美的作品(感谢那个) 但是,因为我想让我的网站更好,我开始添加更多的语言一个新的问题出现了。 我想看看我是否能直接将用户重定向到该网站在他们的语言,如果语言不存在或者cookie设置我想将用户重定向到他们最后的语言或我的默认语言。

That works perfect (thanks for that) But since I want to make my website nicer and I started adding more languages a new problem showed up. I want to see if I can redirect users directly to the site in their language, if this language does not exists or a cookie is set I want to redirect users to their last language or my default language.

现在我在我的.htaccess创造了这个部分。

For now I created this part in my .htaccess

的RewriteCond%{QUERY_STRING} ^ $
  重写规则^ /?$ EN / [L,R = 301]

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?$ en/ [L,R=301]

这将导致所有的呼叫www.xxx.com/被重定向到www.xxx.com/en /

This causes that all calls to www.xxx.com/ are redirected to www.xxx.com/en/

现在的第二部分,在我的PHP code我存储cookie这个cookie设置为www.xxx.com /

Now the second part, in my php code I store a cookie this cookie is set for www.xxx.com/

我试过如下:

的RewriteCond%{} HTTP_COOKIE语言=([^;] +)[NC]
  重写规则^ /?$%1 / [L,R = 302]

RewriteCond %{HTTP_COOKIE} language=([^;]+) [NC]
RewriteRule ^/?$ %1/ [L,R=302]

但是,这给了我一个无限循环。

But this gives me an infinite loop.

我也试过这个,所以如果我的语言是NL重定向到www.xxx.com/nl/~~V但是这也导致了一个无限循环。另外这款code只能在某些情况下可以执行

I also tried this, so if my language is nl redirect to www.xxx.com/nl/ but this also results in an infinite loop. Also this code should only be executed in some cases

的RewriteCond%{HTTP:接受语言}(NL)[NC]
  重写规则^ /?$ NL / [L,R = 302]

RewriteCond %{HTTP:Accept-Language} (nl) [NC]
RewriteRule ^/?$ nl/ [L,R=302]

有人可以帮助我,这是可能的一个htaccess或者我应该在PHP中创建一些逻辑?

Can someone help me and is this possible in an htaccess or should I create some logic in PHP?

1检查cookie设置了语言(我想这应该是上述code)
&NBSP; 2A如果一个cookie设置将用户重定向到该语言
&NBSP; 2B如果没有cookie设置检查HTTP-接受的语言
&NBSP;&NBSP; 2B1如果这种语言存在重定向到语言
&NBSP;&NBSP; 2B2如果语言不存在重定向到默认

1 Check if cookie is set with a language ( I guess this should be the above stated code)
 2a If a cookie is set redirect the user to that language
 2b If no cookie is set check the http-accept language
  2b1 If this language exists redirect to that language
  2b2 If that language does not exists redirect to the default

在此先感谢

推荐答案

接受语言是加权语言标识符的列表。只要有一定的语言识别的发生并不意味着它是最preferred语言。可能有其他语言的更preferred(更高的问:的值),甚至不接受在所有(即 Q = 0 )。

Accept-Language is a list of weighted language identifiers. Just the occurrence of a certain language identifier does not mean that it’s the most preferred language. There might be other languages that are more preferred (higher q value) or even not accepted at all (i.e. q=0).

因此​​,而不是只是在寻找某种语言的标识符,解析接受语言列表中,找到接受的语言和可用语言的最佳匹配,同时考虑到preference订单。既然你似乎可以用PHP,做到这一点用PHP,而不是mod_rewrite的。

So instead of just looking for a certain language identifier, parse the list of accepted languages and find the best match of accepted languages and available languages while considering the preference order. And since you seem to be using PHP, do that with PHP instead of mod_rewrite.

这篇关于多语言网站,基于cookie的重定向或接受语言(重写规则)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆