Apache Geoip重定向到页面,带问号

发布于 2025-02-04 19:16:36 字数 773 浏览 3 评论 0原文

我有一个与GeoIP重定向的绊脚石(Apache/2.4.6(Centos))。

Htaccess具有强制性指令(功能性所需的必要,更改可能会破坏整个站点),

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

因此当我添加.htaccess时,Geoip规则像

RewriteCond "%{ENV:GEOIP_COUNTRY_CODE}" ^DE$
RewriteRule ^/?index\.php$ http://de.example.com/info.html$1 [L]

它一样工作正常,但是当我想将类似的重定向设置为带有翻译的页面时,

RewriteCond "%{ENV:GEOIP_COUNTRY_CODE}" ^DE$
RewriteRule ^/?index\.php$ http://de.example.com/?lang=de$1 [L]

我的错误“重定向太多”。

在.htaccess中禁用geoip,然后直接转到 http://de.example.com/?lang=de - 一切都很好。

已经花了几个小时在语法上玩,但仍然无法遇到问题,所以要尝试任何想法!

I have a stumbling block with GeoIP redirect (Apache/2.4.6 (CentOS))

.htaccess has the mandatory directives (necessary for functionality, change can break the whole site)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

so when I add to .htaccess a GeoIP rule like

RewriteCond "%{ENV:GEOIP_COUNTRY_CODE}" ^DE$
RewriteRule ^/?index\.php$ http://de.example.com/info.html$1 [L]

it works fine, but when I want to setup a similar redirect to a page with translation

RewriteCond "%{ENV:GEOIP_COUNTRY_CODE}" ^DE$
RewriteRule ^/?index\.php$ http://de.example.com/?lang=de$1 [L]

I have error "too many redirects".

Disable GeoIP in .htaccess and go directly to http://de.example.com/?lang=de - all OK.

Already spent few hours playing around syntax but still can't catch what's wrong, so thx for any idea to try !

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

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

发布评论

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

评论(1

千笙结 2025-02-11 19:16:36

您很可能不想进行外部重定向,以将您的内部URL暴露于客户。您想要的(大概)是检测Geoip国家,并基于此添加一个具有相同国家代码的查询参数。您需要添加否定条件以停止重写,当时lang =查询参数已经存在。

您可以在.htaccess中尝试此代码:

RewriteEngine On

RewriteCond "%{ENV:GEOIP_COUNTRY_CODE}" ^DE$
RewriteCond %{QUERY_STRING} !(^|&)lang= [NC]
RewriteRule ^/?(index\.php)?$ $0?lang=de [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

在测试此更改之前,请确保清除浏览器缓存。

Most likely you don't want to do an external redirect that exposes your internal URL to clients. All you want (presumably) is to detect GEOIP country and based on that add a query parameter with same country code. You need to add a negated condition to stop rewrite when lang= query parameter is already present.

You may try this code in your .htaccess:

RewriteEngine On

RewriteCond "%{ENV:GEOIP_COUNTRY_CODE}" ^DE$
RewriteCond %{QUERY_STRING} !(^|&)lang= [NC]
RewriteRule ^/?(index\.php)?$ $0?lang=de [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Make sure to clear your browser cache before testing this change.

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