基于 Accept-Language 的重定向
我需要尊重网络浏览器的语言首选项列表。支持的语言为英语和法语。例如: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会使用 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.
我想目标是按照浏览器中设置的优先顺序与第一种语言进行匹配。我想对于居住在比利时、加拿大、瑞士的人来说,英语版本将是默认版本,法语版本将是默认版本。所以,我建议实现以下内容:如果它首先在接受语言http字段中找到法语,它将返回法语,然后,它会寻找英语。然后,如果既没有英语也没有法语,则返回英语版本。
我不会忘记从重定向中排除 robots.txt,因为它应该位于网站的根文件夹中。
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.