【问题标题】:I want the redirection is similar to the google site [duplicate]我希望重定向类似于谷歌网站[​​重复]
【发布时间】:2013-10-23 20:42:21
【问题描述】:

可能重复:
How to show different homepage based on the user's Country?

我有两个站点,一个是“.com”,第二个是“.in”,但是,当用户来自印度时,我想显示“.in”,如果任何用户来自另一个国家,我想显示他只有“.com”。 这种重定向可能吗,我该怎么做?此重定向类似于 google 站点

【问题讨论】:

  • 这可以通过重定向来完成。您想如何确定用户是否来自印度?
  • 使用IP地址查找国家,然后重定向。

标签: php redirect


【解决方案1】:

您应该检查Accept-Language HTTP 标头(PHP 中的$_SERVER['HTTP_ACCEPT_LANGUAGE']),而不是(或除此之外)查看 IP 地址的国家/地区

PHP intl extension has methods that help parsing it.

此标头通常包含语言-国家/地区对,这是用户可以配置的,与他们的 IP 地址不同。

【讨论】:

    【解决方案2】:

    您可以尝试根据用户的远程地址猜测国家。

    以下是一个可行的解决方案,但您必须使用重定向逻辑:

    $remoteAddr = $_SERVER['REMOTE_ADDR'];
    $lookupUrl = sprintf('http://api.hostip.info/country.php?ip=%s', $remoteAddr);
    $country = trim(file_get_contents($lookupUrl));
    
    if ('IN' === $country)
    {
        $newUrl = 'http://www.vaibhav-kipl.in/';
    
        header(sprintf('Location: %s', $newUrl));
        printf('<a href="%s">Moved.</a>', $newUrl);
        exit();
    }
    

    但请注意,来自印度的搜索引擎机器人也能够抓取 .com 内容。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多