How to show different homepage based on the user&#

2019-01-20 04:42发布

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 it should open www.rajasekar.in

When user from other country types the url in the address bar as www.rajasekar.com it should open www.rajasekar.com

I think this is what implemented in GOOGLE. Please help me in achieving this

5条回答
我欲成王,谁敢阻挡
3楼-- · 2019-01-20 05:18

In ASP.NET, get the client's IP address using :

Request.ServerVariables("REMOTE_ADDR")

Use some IP to geo location service like :

  1. Maxmind
  2. IPAddressExtentions

Once you get the country, you can easily redirect the user :

 Response.Redirect("http://www.yourdomain.in")
查看更多
干净又极端
4楼-- · 2019-01-20 05:26

Instead of (or at least in addition to) looking at country of the IP address, you should inspect Accept-Language HTTP header ($_SERVER['HTTP_ACCEPT_LANGUAGE'] in PHP)

This header usually contains language-country pairs:

en-US, hi_IN, fr, de;q=0.5

PHP intl extension has methods that help parsing it.

Problems with IP lookup:

  • Normal users don't have ability to change their IP address or how their address is classified in IP-geo databases. It's much more likely that users are able to choose/configure language of their operating system/browser, which influences the Accept-Language header.

  • There are ISP proxies that are in country different than the user, e.g. AOL routes some international traffic through US, many mobile users use Opera Mini which has proxy servers in Norway. If you're looking up IP, at least take X-Forwarded-For header into account. Accept-Language header is passed through proxies without such problems.

  • I've been using hostip.info database and found it to be incomplete and contain errors. Also consider that making HTTP request to a 3rd party PHP script each time is going to slow down your site and waste their resources. Parsing of Accept-Language header is essentially free.

  • If you don't want merely country, but language users speaks, then IP→Country won't be accurate for countries where different languages are spoken. Accept-Language may give you more precise information (e.g. nl_BE for Dutch in Belgium, fr_CA for Canadian French).

Neither method is 100% correct, so it would be wise to let users override your selection.

Also keep in mind that if you force redirects to "correct" domain then bots of search engines won't be able to index all your domains, as they will most likely come from an US IP address and won't specify preferred language. It's best not to apply detection to domains other than .com, and/or don't apply it to pages other than the homepage.

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-20 05:39

The Accept-Language request header will tell you what language/region the user has their browser set to.

查看更多
Fickle 薄情
6楼-- · 2019-01-20 05:42

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('<a href="%s">Moved.</a>', $newUrl);
    exit();
}

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

查看更多
登录 后发表回答