【问题标题】:.htaccess : languages redirection.htaccess :语言重定向
【发布时间】:2010-08-06 15:22:03
【问题描述】:

我正在尝试将我博客的访问者重定向到法语或英语版本。 所以我做了这个 .htaccess :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#---------------------
# Language Redirection
#---------------------
# Checking if the redirection didn't occur yet
# Checking that the url doesn't begin with /en
RewriteCond %{REQUEST_URI} !^/en(.*)$

# Checking if the user is in english
RewriteCond %{HTTP:Accept-Language} ^en [NC]

# Redirecting from /the/url to /en/the/url
RewriteRule ^(.*)$ /en/$1 [L,R=301]

#----------------------
# Wordpress Redirection
#----------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

基本上,如果他们是英文的,我想将来自 google 的访问者从 /my/article 重定向到 /en/my/article。 相反,有一个无限循环!由于最后一个 RewriteRule,我认为 REQUEST_URI 始终是 index.php。

有人做过吗?

非常感谢

【问题讨论】:

    标签: apache wordpress .htaccess internationalization redirect


    【解决方案1】:

    Accept-Language 不仅仅是一个相等值的列表;它是一个加权值列表,其中每个值都可以有一个质量值,该值通过 0 到 1 之间的值指定偏好。这意味着仅仅因为一个特定值的出现并不意味着这个值是最优选的.事实上,质量值为 0 意味着“根本不可接受”。

    因此,与其只查看是否存在特定子字符串,不如解析加权值列表并找到首选值和可用值之间的最佳匹配。

    但是 mod_rewrite 不适合这个工作。您最好为此使用更强大的语言,例如 PHP。

    【讨论】:

    • 感谢您的回答秋葵汤。基本上,如果用户将英语放在他的语言列表的开头,我的 .htaccess 中的 RewriteCond 将被填满,因此他将被重定向。
    • @Benjamin Netter:但是以en 开头并不意味着英语是最受欢迎的语言。它的质量值可能不是 1(甚至可能是 0),并且可能有其他语言比英语更受欢迎。那么为什么不使用 PHP 进行语言协商,而不是简单的字符串比较呢?
    • 这不是我想要做的。
    • @Benjamin Netter:我知道。这就是我告诉你这样做的原因。
    猜你喜欢
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2012-04-21
    相关资源
    最近更新 更多