How to show different homepage based on the user's Country?

前端 未结 5 1979
傲寒
傲寒 2020-12-11 11:51

I had two domains. rajasekar.com and rajasekar.in. What I need to do is that,

when a user from India types the url in the address bar as www.rajasekar.com then

5条回答
  •  既然无缘
    2020-12-11 12:21

    You can try to guess the country based on the remote address of the user.

    The following is a working solution, you'll have to work the redirection logic though:

    $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.rajasekar.in/';
    
        header(sprintf('Location: %s', $newUrl));
        printf('Moved.', $newUrl);
        exit();
    }
    

    Take care that search engine robots from India are able to crawl the .com content as well however.

提交回复
热议问题