How to direct/restrict visitor from another country to the same domain but pages only for them?

Hello,

I have seen websites which managed to restrict visitors if they are from a different country.

To be more clear, example - www.domainName.uk and then there is also a www.domainName.th, so if I am in UK and then I want to browse the site at th, I can’t.

As I am exploring something like that, how do I make that happen in my site ?

Do I need to configure the server or I must do it programmtically but how ?

Hope someone can share with me some tips.

Many thanks.

Get the user’s IP address.
Using this IP address, perform an IP Geocoding.
There are lots of sites that offer this API access. like this https://ipstack.com/
Call the API, passing the user’s IP address as a parameter.
You’ll get a response giving you all the details about that IP address.
Something like:

ip: “xxx.xxx.xxx.xxx”
hostname: “xxx-xxx-xxx-xxx-busname-domain.name.net
type: “ipv4”
continent_code: “NA”
continent_name: “North America”
country_code: “US”
country_name: “United States”
region_code: “TN”
region_name: “Tennessee”
city: “Clarksville”
zip: 37040
latitude: 36.5246
longitude: -87.3465

Check the region or the country code, and reroute user to the appropriate page or website.

NOTE: The Geocoding result may not be accurate, because it’s a guesstimate on whose Internet company that IP address block is assigned to. If your ISP is in another city different from yours, Geocoding your IP will display the ISP’s city, not yours. But for your purpose, that’s good enough.