如何使用 htaccess 将国际用户重定向到英文版网站?

发布于 2025-01-07 06:00:21 字数 446 浏览 3 评论 0原文

我正在执行第一个 htaccess 步骤,但无处可去...

我有一个德语和英语网站,我想将所有非德语用户重定向到该网站的英语版本。

这是我的问题:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:Accept-Language} (cs|da|el|en|es|et|fi|fr|ga|hr|hu|it|lt|lv|nl|no|pl|pt|ro|ru|sl|sr|sv) [NC]
RewriteRule ^$ http://www.some.de/index.cfm?language=EN [R,L]

两个问题:
我在德国,浏览器设置为德语,但仍然重定向到英语版本。这是为什么?
如果用户从某个较低级别的页面开始,是否必须对所有后续页面执行此操作?我必须为每个页面制定重写规则吗?

I'm doing my first htaccess steps and going nowhere...

I have a site in German and English and I want to redirect all non German-speaking users to the English version of the site.

Here is what I have:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:Accept-Language} (cs|da|el|en|es|et|fi|fr|ga|hr|hu|it|lt|lv|nl|no|pl|pt|ro|ru|sl|sr|sv) [NC]
RewriteRule ^$ http://www.some.de/index.cfm?language=EN [R,L]

Two questions:
I'm in Germany with a browser set to German and I'm still redirected to the Englisch version. Why is that?
Will this have to be done for all subsequent pages in case the user starts out on some lower level page? Do I have to make a rewrite rule for every page?

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

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

发布评论

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

评论(1

花期渐远 2025-01-14 06:00:21

我在德国,浏览器设置为德语,但仍然重定向到英语版本。这是为什么?

接受语言是语言环境和价值观的大杂烩。您的浏览器可能还会提交一堆在您的正则表达式中匹配的其他语言/语言环境对(例如 en-ca,en;q=0.8,en-us;q=0.6,de-de;q= 0.4,de;q=0.2)。您可以尝试做的是匹配 not de

RewriteCond %{HTTP:Accept-Language} !de [NC]

如果用户从某个较低级别的页面开始,是否必须对所有后续页面执行此操作?我必须为每个页面制定重写规则吗?

不确定您如何设置页面,但您可以尝试将规则更改为:

RewriteRule ^(.*)$ /$1?language=EN [R,L,QSA]

因此 / 的请求将重定向到 /?language=EN,并且 < code>/somepath/ 到 /somepath/?language=EN,以及 /file.cfm?someparam=value/file.cfm?语言=EN&someparam=值

I'm in Germany with a browser set to German and I'm still redirected to the Englisch version. Why is that?

Accept-Language is a mish-mash of language locales and values. Your browser probably also submits a bunch of other lang/locale pairs that gets matched in your regex (e.g. something like en-ca,en;q=0.8,en-us;q=0.6,de-de;q=0.4,de;q=0.2). What you could try doing is matching for not de:

RewriteCond %{HTTP:Accept-Language} !de [NC]

Will this have to be done for all subsequent pages in case the user starts out on some lower level page? Do I have to make a rewrite rule for every page?

Not sure how you've got your pages setup, but you could try changing your rule to this:

RewriteRule ^(.*)$ /$1?language=EN [R,L,QSA]

So requests for / gets redirected to /?language=EN, and /somepath/ to /somepath/?language=EN, and /file.cfm?someparam=value to /file.cfm?language=EN&someparam=value

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