Redirecting Domains Based on User IP Address by Country
This guide addresses the issue of automatically redirecting users to specified subdomains based on their IP address to cater to different geographical regions.
To achieve this, a widely used method involves utilizing the geoPlugin class. The class can be downloaded from http://www.geoplugin.com/_media/webservices/geoplugin.class.phps.
Redirection Implementation
Create an index.php file in your root folder and include the following code:
<code class="php"><?php require_once('geoplugin.class.php'); $geoplugin = new geoPlugin(); $geoplugin->locate(); // Country code variable $var_country_code = $geoplugin->countryCode; // Redirection based on country code if ($var_country_code == "AL") { header('Location: http://sq.wikipedia.org/'); } elseif ($var_country_code == "NL") { header('Location: http://nl.wikipedia.org/'); } else { header('Location: http://en.wikipedia.org/'); } ?></code>
The script locates the user's IP address and retrieves the corresponding country code. Based on the code, the user is redirected to the appropriate subdomain.
Country Code Reference
A comprehensive list of country codes can be found at http://www.geoplugin.com/iso3166. Refer to this list to specify the subdomains for each country.
以上是如何根據使用者的 IP 位址和國家/地區自動將使用者重新導向到特定子網域?的詳細內容。更多資訊請關注PHP中文網其他相關文章!