[英]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: 这是您将遇到的其他一些问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.