简体   繁体   English

如何使用htaccess添加多语言支持重写规则?

[英]How to add multilingual support rewrite rules using htaccess?

My website urls structure is based like this 我的网站网址结构是这样的

www.domain.com/page1 -> default language

where all my pages are located in the root folder. 我的所有页面都位于根文件夹中。

I want to add multilingual support and create folders for each language, containing the file with the translated text. 我想添加多语言支持并为每种语言创建文件夹,其中包含带有翻译文本的文件。

So when a language is requested, looks in the appropriate language folder and display it. 因此,当请求一种语言时,请在相应的语言文件夹中查找并显示它。

www.domain.com/lang/page1 -> 2 chars language

How can I do this, keeping the english language as the default language? 保持英语为默认语言,我该怎么做?

Currently this is my htaccess 目前这是我的htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ErrorDocument 404 /404.php
RewriteRule ^([^\.]+)$ $1.php [NC,L]

You can use language redirection by .htaccess. 您可以通过.htaccess使用语言重定向。 An example for German: 德语的示例:

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule .* https://google.de [L]

Why are not you just using PHP? 为什么不只是使用PHP? You can get the browser language, just look at this function: 您可以获取浏览器语言,只需查看以下功能即可:

public static function getBrowser ($languages, $accept) {
    // HTTP_ACCEPT_LANGUAGE is defined in
    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
    // pattern to find is therefore something like this:
    //    1#( language-range [ ";" "q" "=" qvalue ] )
    // where:
    //    language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
    //    qvalue         = ( "0" [ "." 0*3DIGIT ] )
    //            | ( "1" [ "." 0*3("0") ] )
    preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" .
                   "(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i",
                   $accept, $hits, PREG_SET_ORDER);

    // default language (in case of no hits) is the first in the array
    $bestlang = $languages[0];
    $bestqval = 0;

    foreach ($hits as $arr) {
        // read data from the array of this hit
        $langprefix = strtolower ($arr[1]);
        if (!empty($arr[3])) {
            $langrange = strtolower ($arr[3]);
            $language = $langprefix . "-" . $langrange;
        }
        else $language = $langprefix;
        $qvalue = 1.0;
        if (!empty($arr[5])) $qvalue = floatval($arr[5]);

        // find q-maximal language
        if (in_array($language,$languages) && ($qvalue > $bestqval)) {
            $bestlang = $language;
            $bestqval = $qvalue;
        }
        // if no direct hit, try the prefix only but decrease q-value
        // by 10% (as http_negotiate_language does)
        else if (in_array($languageprefix,$languages) 
            && (($qvalue*0.9) > $bestqval)) 
        {
            $bestlang = $languageprefix;
            $bestqval = $qvalue*0.9;
        }
    }
    return $bestlang;
}

I use it myself with this call: 我自己通过此电话使用它:

$browserLanguage = getBrowser($supportedLanguages, $_SERVER['HTTP_ACCEPT_LANGUAGES'];

For $supportedLanguages , you can use codes like de , en-gb or en-us . 对于$supportedLanguages ,您可以使用deen-gben-us

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM