How to redirect visitors from a specific country to the website of their choice?

Are you looking for a solution to redirect visitors from a specific country to the localized version of your website? Do you want to know how to redirect certain visitors from different countries based on geographic information? Then this post will help you with the best solutions.

In a scenario involving multiple language-based websites, automatic redirection from one to another is possible. This situation occurs frequently when shopping online because not only is a different language required, but the offer can also be customized for the target market.

Tutorial

Let's say you have three domains
my-domain.de
my-domain.fr
my-domain.com

Each with its own website aimed at a specific local market. The first site is intended for German visitors only; the second has been set up for French speakers, and the third is available to all other users worldwide who access the web in English.

Your aim is therefore to show visitors the right website for their country of origin. In this case, you can help yourself with a command that you enter in the .htaccess file.

💡
The domain's or subdomain's Root Directory is where the .htaccess file may be found. You must have the cPanel control panel or an FTP client with the ability to show hidden files in order to access this hidden file. You must first build a .htaccess file if you don't already have one.

To achieve the result we described above, you can use the following code:

<IfModule mod_maxminddb.c>

MaxMindDBEnable On

# Redirect German & Austrian IP addresses to .de domain

RewriteCond %{HTTP_HOST} (.*)?my-domain\.(fr|com) [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(DE|AT)$
RewriteRule ^(.*)$ https://my-domain.de/$1 [R,L]

# Redirect French IP addresses to .fr domain

RewriteCond %{HTTP_HOST} (.*)?my-domain\.(de|com) [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^FR$
RewriteRule ^(.*)$ https://my-domain.fr/$1 [R,L]

# Redirect all other IP addresses to .COM domain

RewriteCond %{HTTP_HOST} (.*)?my-domain\.(de|fr) [NC]
RewriteRule ^(.*)$ https://my-domain.com/$1 [R,L]

</IfModule>
.htaccess file

For a table of the two-letter country codes, see: https://www.iban.com/country-codes

Conclusion

Redirecting users based on their location is standard practice when it comes to operating a business online. You want to ensure that your website is accessible or at least available in every country around the globe where it makes sense and in every location where you are active. Redirects will be one of the best solutions to this problem as they are easy to set up, very powerful, and simple to manage.