基于 Accept-Language 的重定向

发布于 2024-09-03 02:19:22 字数 132 浏览 1 评论 0原文

我需要尊重网络浏览器的语言首选项列表。支持的语言为英语和法语。例如:http_accept_language="jp-JP;fr;en-US;en" 重定向到名为 /French/ 的目录。如何使用 .htaccess 文件中的重写规则来做到这一点?

I need to honor the web browser's list of language preferences. Supported languages are English and French. For example: http_accept_language="jp-JP;fr;en-US;en" redirects to a directory called /French/. How can I do this with rewrite rules in my .htaccess file?

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

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

发布评论

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

评论(2

南风几经秋 2024-09-10 02:19:22

我不会使用 mod_rewrite 来实现此目的,而是使用更强大的语言。因为 Accept-Language 是一个加权值列表(请参阅 质量值)并且其中一个标识符的出现并不意味着它优于另一个值(特别是q=0意味着根本不可接受)。

正如已经说过的,使用比 mod_rewrite 更强大的语言,解析值列表并找到首选选项和可用选项的最佳匹配。

I wouldn’t use mod_rewrite for this but a more powerful language. Because Accept-Language is a list of weighted values (see quality value) and the occurrence of one of the identifiers does not mean that it’s preferred over another value (especially q=0 means not acceptable at all).

As already said, use a more powerful language than mod_rewrite, parse the list of value and find the best match of preferred options and available options.

吾性傲以野 2024-09-10 02:19:22

我想目标是按照浏览器中设置的优先顺序与第一种语言进行匹配。我想对于居住在比利时、加拿大、瑞士的人来说,英语版本将是默认版本,法语版本将是默认版本。所以,我建议实现以下内容:如果它首先在接受语言http字段中找到法语,它将返回法语,然后,它会寻找英语。然后,如果既没有英语也没有法语,则返回英语版本。
我不会忘记从重定向中排除 robots.txt,因为它应该位于网站的根文件夹中。

RewriteEngine On
#exclude robots.txt 
RewriteCond %{REQUEST_FILENAME} robots.txt
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} fr [NC]
RewriteRule ^$ https://nicolasgueri.net/fr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} en [NC]
RewriteRule ^$ https://nicolasgueri.net/en/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^.*$ [NC]
RewriteRule ^$ https://nicolasgueri.net/en/ [L,R=301]

I guess the goal would be to match with the first language in priority order set up in the browser. I guess the english version would be the default version and the french version for people living in Belgium, Canada, Switzerland. So, i would suggest to implement the following: It will return french if it finds french somewhere first in accept-language http field, then, it would look for english. Then, if there is neither english nor french, it would return the english version.
I don't forget to exclude the robots.txt from the redirection as it should be in root folder of the website.

RewriteEngine On
#exclude robots.txt 
RewriteCond %{REQUEST_FILENAME} robots.txt
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} fr [NC]
RewriteRule ^$ https://nicolasgueri.net/fr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} en [NC]
RewriteRule ^$ https://nicolasgueri.net/en/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^.*$ [NC]
RewriteRule ^$ https://nicolasgueri.net/en/ [L,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文