久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 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 重定向的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 乱码av午夜噜噜噜噜动漫 | 久在线 | 亚洲欧美日韩中文在线 | www.887色视频免费 | aaa在线观看| 草草影院ccyy| 欧美一级片在线 | 国产二区av | 天天宗合网| 亚洲不卡在线观看 | av男人的天堂在线 | 国产亚洲精品美女久久久久久久久久 | www.av在线 | 亚洲美女一区二区三区 | 最新国产精品精品视频 | 在线色网| 日韩成人在线播放 | 综合网中文字幕 | 精品亚洲一区二区三区 | 一级黄色生活视频 | 日本a∨精品中文字幕在线 亚洲91视频 | 天堂一区二区三区 | 欧美一区二区在线观看 | 91在线视频播放 | 超碰97人人人人人蜜桃 | 免费一区二区在线观看 | 97日韩精品 | 久久91精品 | 国产精品久久久久久久久免费桃花 | 综合久久网 | 青青草社区 | 成人乱人乱一区二区三区软件 | 日韩精品一区二区三区中文在线 | 久久久久久久久久久久久九 | 91精品久久久久久久久 | 北条麻妃一区二区三区在线视频 | 日本不卡一区二区三区 | 亚洲国产一区二区视频 | 亚洲精品久久久一区二区三区 | 91视频.| 日韩免费一二三区 |