多语言站点,基于 cookie 或接受语言(RewriteRule)的重定向

发布于 2024-09-18 18:15:58 字数 1101 浏览 2 评论 0原文

几周前,我问了一个有关多语言网站的问题(请参阅多语言网站,子目录作为语言(RewriteRule))。

效果很完美(谢谢) 但由于我想让我的网站变得更好并且我开始添加更多语言,所以出现了一个新问题。 我想看看是否可以将用户直接重定向到其语言的网站,如果该语言不存在或设置了 cookie,我想将用户重定向到他们的最后语言或我的默认语言。

现在我在 .htaccess 中创建了这部分

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

这会导致所有对 www.xxx.com/ 的调用都重定向到 www.xxx.com/en/

现在是第二部分,在我的 php 代码中我存储了一个 cookie这个cookie是为www.xxx.com/设置的

我尝试了以下操作:

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

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

我也尝试过这个,所以如果我的语言是 nl 重定向到 www.xxx.com/nl/ 但这也会导致无限循环。此外,此代码只应在某些情况下执行

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

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

1 检查cookie是否设置了某种语言(我想这应该是上面所说的代码)
2a 如果设置了 cookie,则将用户重定向到该语言
2b 如果未设置 cookie,请检查 http 接受语言
  2b1 如果该语言存在则重定向到该语言
  2b2 如果该语言不存在,则重定向到默认语言

提前致谢

A few weeks ago i asked a question about a multilanguage site (see Multilanguage site, subdirectories as language (RewriteRule)).

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.

For now I created this part in my .htaccess

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

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

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

I tried the following:

RewriteCond %{HTTP_COOKIE} language=([^;]+) [NC]

RewriteRule ^/?$ %1/ [L,R=302]

But this gives me an infinite loop.

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:Accept-Language} (nl) [NC]
RewriteRule ^/?$ nl/ [L,R=302]

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

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

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

城歌 2024-09-25 18:15:58

接受语言是加权语言标识符的列表。仅出现某种语言标识符并不意味着它是最首选的语言。可能还有其他语言更受青睐(q 值较高),甚至根本不被接受(即 q=0)。

因此,不要只是寻找某种语言标识符,而是要解析接受的语言列表,并在考虑优先顺序的同时找到接受的语言和可用语言的最佳匹配。由于您似乎正在使用 PHP,因此请使用 PHP 而不是 mod_rewrite 来执行此操作。

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文