Redirecting Domains Based on User IP Country
Problem:
You wish to automatically redirect users to specific subdomains based on their country's IP address. For instance, if a user from India visits abcd.com, they should be redirected to ind.abcd.com.
Solution:
To achieve country-specific redirection, you can utilize the GeoPlugin library. Follow these steps to implement it:
<code class="php"><?php require_once('geoplugin.class.php'); $geoplugin = new geoPlugin(); $geoplugin->locate(); // Country code variable $var_country_code = $geoplugin->countryCode; // Set redirect based on country code: switch ($var_country_code) { case "AL": header('Location: http://sq.wikipedia.org/'); break; case "NL": header('Location: http://nl.wikipedia.org/'); break; default: header('Location: http://en.wikipedia.org/'); } ?></code>
This script will detect the user's IP address and automatically redirect them to the appropriate subdomain based on their country.
The above is the detailed content of How to Automatically Redirect Domain Visitors Based on Their IP Country?. For more information, please follow other related articles on the PHP Chinese website!