htaccess语言检测返回无限重定向 - 谁发现了错误?

时间:2013-08-15 07:51:05

标签: .htaccess language-detection

我有一个survey community有3种语言,使用WordPress的WPML插件来处理这些语言。不幸的是语言检测只能通过PHP工作。我想使用htaccess,因为它更快,用户也不会注意到延迟。

设置如下:

community.netigate.net/ (English, International)

community.netigate.net/de/ (German)

community.netigate.net/sv/ (Swedish)

我尝试了不同的方法,并找到了最好的方法

  

http://tech-blog.borychowski.com/index.php/2009/03/htaccess/redirect-according-to-browser-language-mod-rewrite-and-http_accept_language/

由于大多数用户都是德国人或瑞典人,我希望“英语”只是标准的后备语言。设置可能是这样的:

  

检查语言是否为瑞典语,然后重定向到瑞典子页面     检查如果语言是德语,那么重定向到德语子页面     ELSE使用英语作为后备

不幸的是,下面的解决方案最终会导致无休止的重定向?我错过了什么吗?

## Language Detection

#The 'Accept-Language' header starts with 'sv'
#and the test is case-insensitive ([NC])

RewriteCond %{HTTP:Accept-Language} ^sv [NC]

#Redirect user to /sv/hauptseite address
#sending 301 (Moved Permanently) HTTP status code

RewriteRule ^$ /sv/ [L,R=301]

#The 'Accept-Language' header starts with 'de'
#and the test is case-insensitive ([NC])

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

#Redirect user to /de/hauptseite address
#sending 301 (Moved Permanently) HTTP status code

RewriteRule ^$ /de/ [L,R=301]

#For every other language (including English :)) use English

RewriteRule ^$ / [L,R=301]

2 个答案:

答案 0 :(得分:0)

添加另一个重写条件,检查重定向是否已经发生

## Language Detection

#The 'Accept-Language' header starts with 'sv'
#and the test is case-insensitive ([NC])

RewriteCond %{HTTP:Accept-Language} ^sv [NC]

#If not already redirected
RewriteCond %{REQUEST_URI} !^/sv/ [NC]    # ADDED

#Redirect user to /sv/hauptseite address
#sending 301 (Moved Permanently) HTTP status code

RewriteRule ^$ /sv/ [L,R=301]

#The 'Accept-Language' header starts with 'de'
#and the test is case-insensitive ([NC])

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

#If not already redirected
RewriteCond %{REQUEST_URI} !^/de/ [NC]    # ADDED

#Redirect user to /de/hauptseite address
#sending 301 (Moved Permanently) HTTP status code

RewriteRule ^$ /de/ [L,R=301]

#For every other language (including English :)) use English

RewriteRule ^$ - [L]    # MODIFIED

答案 1 :(得分:0)

这一行:

#For every other language (including English :)) use English
RewriteRule ^$ / [L,R=301]

/重定向回/,导致重定向循环。你根本不需要这条线。