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

How to redirect based on Accept-Language with Apache / mod_rewrite(如何使用 Apache/mod_rewrite 基于 Accept-Language 重定向)
本文介绍了如何使用 Apache/mod_rewrite 基于 Accept-Language 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

对于语言重定向,我们目前在 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.

这篇关于如何使用 Apache/mod_rewrite 基于 Accept-Language 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)