Redirect website according country

I need help redirecting my website based on visitor ip address (location).

I have 3 specific websites

I want to redirect visitors from India to mysite.com/IN

I want to redirect visitors from USA to mysite.com/US

I want to redirect visitors from Thailand of the world to mysite.com/THA

I tried several codes and modifying htaccess as well.

We can also check other country content by Change country from given dropdown.

as I understand I have to modify Base url as per country set.

Can anyone please guide me for the same?

Hi,

You may refer to this site for free API call to identify the user country based on their IP address.

http://www.geoplugin.com/

This is the example code for checking it.

Once you found the country name then use PHP’s header to redirect to particular website. sample code is below this may be helpful





$geoplugin_countryCode = $res['geoplugin_countryCode']; //$res is your response from geoplugin web service.

header("Location: http://mysite.com/$geoplugin_countryCode");



header("Location: http://mysite.com/$geoplugin_countryCode");

Where to write this code in Yii2 files?

and I also have to modify baseUrl. right?

Hi,

You may create a new yii2 component and add it to your config/web.php file and write the above code into a new method and invoke the method on each new request.

For more info refer the following link

https://www.yiiframework.com/doc/api/2.0/yii-base-application#$bootstrap-detail