如何使用 Apache/mod_rewrite 基于 Accept-Language 重定向

2021-12-21 00:00:00 redirect php .htaccess apache mod-rewrite

对于语言重定向,我们目前在 Web 根目录中创建文件夹,其中包含检查 HTTP_ACCEPT_LANGUAGE 服务器变量的 index.php 文件.例如对于网址 www.example.com/press/

For language redirects we currently create folders in the web root containing an index.php file which checks the HTTP_ACCEPT_LANGUAGE server variable. e.g. for the url www.example.com/press/

/var/www/site/press/index.php:

<?php
  if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "en")
    header("location: ../press_en.php");
  else 
    header("location: ../press_de.php");
?>

随着网站的发展,我们现在有很多这样的文件夹.我试图通过将重定向移动到单个 .htaccess 文件来清理它:

As the site has grown, we now have many such folders. I am trying to clean this up by moving the redirects to a single .htaccess file:

RewriteEngine on

# Set the base path here
RewriteBase /path/to/site/

# The 'Accept-Language' header starts with 'en'
RewriteCond %{HTTP:Accept-Language} (^en) [NC]

# EN redirects
RewriteRule press(/?)$   press_en.php [L,R]

# DE redirects (for all languages not EN)
RewriteRule press(/?)$   press_de.php [L,R]

思路和php文件一样,但是不行.我已经在 Firefox 首选项中尝试了所有可能的语言设置/顺序,并检查了标题是否正确,但它始终为 press_de.php 文件提供服务.

The idea is the same as the php file, but it doesn't work. I have tried all the possible language settings / orders in Firefox preferences, and checked the headers are correct, but it always serves the press_de.php file.

我做错了什么,还是有更好的方法?(不包括内容协商/多视图或任何需要重命名文件的内容,目前这不是一个选项).

What am I doing wrong, or is there a better way? (not including content negotiation / multiviews or anything that requires renaming files, this is not currently an option).

推荐答案

我会将语言指示符放在 URL 路径的开头,例如 /en/.../de/....然后,您可以使用单个脚本来检查首选语言并通过添加语言指示符来重定向请求:

I would put the language indicator at the start of the URL path like /en/… or /de/…. Then you can use a single script that checks the preferred language and redirects the request by prepending the language indicator:

// negotiate-language.php
$availableLanguages = array('en', 'de');
if (!preg_match('~^/[a-z]{2}/~', $_SERVER['REQUEST_URI'])) {
    $preferedLanguage = someFunctionToDeterminThePreferedLanguage();
    if (in_array($preferedLanguage, $availableLanguages)) {
        header('Location: http://example.com/'.$preferedLanguage.$_SERVER['REQUEST_URI']);
    } else {
        // language negotiation failed!
        header($_SERVER['SERVER_PROTOCOL'].' 300 Multiple Choices', true, 300);
        // send a document with a list of the available language representations of REQUEST_URI
    }
    exit;
}

以及相应的规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ negotiate-language.php [L]
RewriteRule ^([a-z]{2})/([^/]+)$ $2_$1.php [L]

请注意,您需要一个正确的 someFunctionToDeterminThePreferedLanguage 函数作为 Accept-Language 标头字段 不是单个值,而是限定值的列表.所以可能有多个值,第一个值并不总是首选值.

Note that you need a proper someFunctionToDeterminThePreferedLanguage function as Accept-Language header field is not a single value but a list of qualified values. So there might be more than just one value and the first value is not always the prefered value.

相关文章