【问题标题】:how to change domain name according to country wise in wordpress如何在wordpress中根据国家/地区更改域名
【发布时间】:2017-04-10 10:11:30
【问题描述】:

我的 WordPress 网站托管在 nginx 服务器上。现在,我创建了一个子域名,例如 us.example.com 当网站在美国开放时,我如何根据国家/地区更改 WordPress URL,即 us.example.com?

【问题讨论】:

    标签: php wordpress nginx


    【解决方案1】:

    有一个网站提供给定ip的地理位置(实际上有很多,但我只关注这个):http://ip-api.com/

    您可以使用他们提供的信息将您的访问者重定向到适当的子域,如下所示:

    $this_ip = $_SERVER["REMOTE_ADDR"];
    $get_ip_info = file_get_contents("http://ip-api.com/json/".$this_ip);
    $ip_info = json_decode($get_ip_info);
    $countryCode = strtolower($ip_info->{"countryCode"});
    
    header("Location: http://".$countryCode.".example.com");    
    

    当然,有很多国家,因为实际上不可能为每个国家维护一个子域,您可以使用“if”语句来缩小重定向选项:

    if($countryCode=="fr"){ //french
        header("Location: http://fr.example.com");
    }else
    if($countryCode=="de"){ //german
        header("Location: http://de.example.com");
    }else
    if($countryCode=="gr"){ //greek
        header("Location: http://gr.example.com");
    }else{ //universal
        header("Location: http://www.example.com");
    }
    

    希望对您有所帮助!

    【讨论】:

    • 它不工作。当我们将它重定向到子域时,该站点没有打开.. 实际上
    • 如果你直接去(例如)de.example.com 网站可以工作,但是如果你去那里使用我的脚本不工作?是这个意思吗?
    • 首先尝试“回显”变量“countryCode”。然后确保与该值对应的子域存在于您的项目中...
    • 我有一个带有 example.com 域的 wordpress 网站,并且在数据库中 siteurl 是 example.com。我创建了一个子域,即 us.example.com,子域中没有安装 wordpress。现在,我希望当我打开 us.example.com 时,该站点将打开此域 example.com 中的站点,但域名显示 us.example.com。我希望你明白我在问什么。我卡住了,请帮帮我
    • 这种情况下,你需要根据子域重写url,重定向到你的主域。这看起来像一个 nginx 问题,我建议您阅读以下内容:stackoverflow.com/questions/33443290/…(或此:stackoverflow.com/questions/15266253/…
    猜你喜欢
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2019-04-15
    • 1970-01-01
    相关资源
    最近更新 更多