简体   繁体   English

从根域到子域的wordpress重定向

[英]wordpress redirection from root domain to a subdomain

Description of the environment: one Wordpress multisite with the root site example.com and multiple subdomains like en.example.com, es.example ... 环境描述:一个Wordpress多站点,具有根站点example.com和多个子域,例如en.example.com,es.example ...

Question: how to get the user to the right subdomain based on the language of the browser? 问题:如何根据浏览器的语言使用户进入正确的子域?

Plugins found until now: Wordpress redirect whom description contains the following: 到目前为止发现的插件:Wordpress重定向,其描述包含以下内容:

WordPress plugin which redirects from the root site of a multisite project to a language specific network site. WordPress插件,可从多站点项目的根站点重定向到特定于语言的网络站点。

Please Note: This plugin doesn't allow content on the root site! 请注意:此插件不允许根网站上的内容! Please read this description carefully to see if this works for you. 请仔细阅读此说明,看看它是否适合您。

it seems that this redirection plugin is for redirection to subfolders, I am a newbie to wordpress and it is quite long time ago where I have done something with PHP, but it should not be so complicated I guess.. 看来,这个重定向插件是用于重定向到子文件夹的,我是wordpress的新手,而且很长时间以前,我已经用PHP做过一些事情,但是我想它应该没有那么复杂。

Your architecture is wrong, WordPress multisite is not used for language translation unless you want to have an entirely different design/theme for your other language, you should use a plugin like Polylang (Free) instead, you can use subdomains if you want! 您的体系结构是错误的,除非您希望其他语言具有完全不同的设计/主题,否则WordPress多站点将不用于语言翻译,而应使用Polylang(免费)之类的插件,而您可以使用子域!

But just to answer your question, if you continue using your current multisite architecture you would use a function like this: 但是,只是为了回答您的问题,如果继续使用当前的多站点体系结构,您将使用以下函数:

function redirectOtherLanguages(){

    if(!isReturningVisitor())
    {
        $domain = getDomain();
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        $newAddress = 'http://'.$lang.'.'.$domain.$_SERVER['REQUEST_URI'];
        echo $newAddress; die();
        header("Location: ".$newAddress);//include check session FR
    }
}

function getDomain(){
    $parts=explode('.', $_SERVER["SERVER_NAME"]);
    $potentialSubDomain = array_shift($parts);
    //print_r($parts); die();
    if(!empty($parts)){
        if(count($parts)>=2) return implode('.',$parts);
        else return $_SERVER["SERVER_NAME"];
    }
    else return null;
}

//This function prevents redirecting returning users
function isReturningVisitor(){

    if(empty($_COOKIE['returning_visitor'])) 
    {
        setcookie("returning", TRUE, time()+60*60*24*365); // Expires in a year
        return false;       
    }
    return true;       
}

redirectOtherLanguages();

Here are some other problems you will encounter: 这是您将遇到的其他一些问题:

  1. Since each language is a different site, you will need to replicate every post, page, etc. 由于每种语言是一个不同的站点,因此您将需要复制每个帖子,页面等。
  2. You will have problems id your website has other subdomains rather than the language itself, you wi please think about using Polylang or any other approach. 您可能会遇到问题,即您的网站具有其他子域而非语言本身,请考虑使用Polylang或任何其他方法。
  3. You need to also detect if the page in that language exists before redirecting (to unwanted avoid 404's). 您还需要在重定向之前检测该语言页面是否存在(避免出现404错误)。
  4. You will need to specify the list of supported languages in an array, and specify your default language if the user language is not found on the array. 您将需要在数组中指定支持的语言列表,如果在数组中未找到用户语言,则需要指定默认语言。
  5. Etc!!!!!!!!!!!!!!!!!!!!! 等等!!!!!!!!!!!!!!!!!!!!!

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

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