How to redirect by country, region or IP in WordPress

There are times when you will need to make redirects to certain visitors, based on their IP, region or country, and if you are not familiar with geolocation databases and editing server files such as .htaccess it will be complicated.

Reasons for having to redirect by country, region or IP

map locations image

There can be several reasons why you want to redirect your visitors to a specific URL.

For example, let’s say you have an e-commerce site and you offer different purchase plans for some of your products in different countries or regions. And you clearly don’t want people from a particular country or region to be able to see the price of the same product from a different country or region.

There are other types of situations where, for example, you simply want to block visitors from certain countries from accessing your website, redirecting them to a black hole, or another random website, either for revenue optimization or even for security reasons. Imagine that all the visits you receive from a certain IP or country only come to try to brute force access to your site, then you just redirect them out of your domain.

The reasons and situations can be very varied.

And I am not referring to redirect only your domain to another URL according to the country, but to redirect any page of your site to another URL.

The easiest way to redirect visitors by country, region, or IP address

As you may have guessed, the easiest method to make redirections geolocating your visitors by country, region or IP is using plugins.

Well, after many years of testing all the WordPress plugins available for this purpose, it turned out that the most effective is a free plugin: IP2Location Redirection.

I have been using it for a long time in my own and clients’ websites and the results are perfect. It has a paid version, which offers a commercial geolocation database, but so far I have never needed it, because it works perfectly with the free geolocation database, or LITE.

But let’s get down to business, you’ll see how easy it is to make redirects by country, region or IP.

The first thing is to install the IP2Location Redirection plugin, like any other, and activate it.

When it is active you will see a new menu called IP Redirection, and as soon as you access the plugin itself you will be greeted with a simple wizard, in which you only have to click the one button to get started.

IP2location wizard start

And the first thing we will be asked to do is to register (for free) on the service’s website, to obtain a token for the LITE geolocation database.

IP2location wizard account creation and token

This is essential so that, from now on, the (essential) updates of the geolocation database are done automatically, and basically for the plugin to work. I wish everything was so simple and cheap, and don’t worry, they don’t send any spam, in fact they never send emails.

Click on the link I show you in the above screenshot and register on their website. When you finish the registration and verification of the email you will get your token or you will find in the “Database Download” screen with the database download links. For the moment just copy the token.

And then paste it into the IP2Location wizard screen and click on the continue button, the database download process will start automatically, until the process is finished.

IP2location wizard end

Click on the Next button, you have already finished downloading the geolocation database.

Important adjustments

However, it is important to go through the settings before starting to create rules for several reasons:

  • Configure some recommended settings
  • Update the geolocation database
IP2location settings

In this screen you will find the following, which I will explain:

  1. Lookup mode – Here you choose the way to update the geolocation database. By default it is “Local BIN Database”, the free version, but if you paid for the PRO version it changes to “API Web Service”, although I already told you that it is almost never necessary.
  2. Download token – Your download token, the one you added in the wizard, and that you can add later here if you skipped the wizard.
  3. Enable Region Database – If you need to redirect visitors to specific regions of countries (recommended) enable this option and then update the database in the button below.
  4. Download IP4 database only – If enabled (default) download a light version of the geolocation database. My advice is to try both versions, but this option is the one that detects IPs the fastest.
  5. Update database – Button to update to the latest version of the geolocation database. It is recommended to update it once a month, but you will receive an email from the service to remind you.
  6. Enable debugging Log – If you have problems with the service enable this option and a log file will be created and you may be asked by the plugin support team.

When you are done save the changes.

Redirection rules by country or region

From here begins your work of creating redirection rules, in the screen called “Rules“, where you have a series of options and fields, which I summarize in the following screenshot…

IP2location rules

Apart from the settings explained in the screenshot above, the real potential of the plugin is in the list of rules you can add, where you will choose:

  1. Location – Country/Region/IP of the visitors
  2. From – Page to which the visitors arrive
  3. Destination – Page or URL to which you will redirect visitors to
  4. Type of redirection – 301 (permanent) or 302 (temporary)
  5. Active or inactive redirection – In case you want to simply create them for the moment and activate them when needed, at a later time.

Things to remember

Just a quick reminder because some people can get confused by this:

IP2location important rule

Just look at the part I highlighted in red because is the most important part of your redirections and the one that can give you the most problems. Because maybe you get it at first but when you have to teach 2 or 3 people from your team 1 can get it wrong and nothing will work like it should:

  1. If you want to redirect some pages from one or a few countries, you choose the countries to redirect and you DO NOT check the box below.
  2. If you want to redirect from most countries except a few ones, you choose the countries that you don’t want to redirect and then CHECK the box below.

It might seem obvious but if I tell you is because it has happened to some clients who had it and when we went to check it this was the problem.

Geolocate IPs

Finally, in case you ever need to check where an IP is from, you have an additional screen called “IP Lookup” where you can check it.

IP2location ip lookup

Redirects and caching

Before finishing, you should know that the plugin developers indicate that the redirects will not work if you have any cache plugin active, any, and it is half true.

And I say “half true” because although it is technically true that if you have a cache plugin active by default they will not work, you can live with the geolocation redirects and the cache if you make exceptions, and many cache plugins allow it.

Of course, you can disable caching altogether, but if your plugin allows it there is another way to use both technologies.

The trick is to add cache exceptions for pages to which you have added geolocation redirects.

sg optimizer caching

How to check if redirects are working

Once you have created your rules you will want to check if they work, and of course, you are not in all countries, it is not even easy to have acquaintances in all the countries you want to check rules.

One method, if you know how to change the circuit at will, you can use the private Tor Browser, but it is not easy to choose from which IP to connect, since by default the connection chooses random IPs, precisely to protect your privacy.

Another way to test your redirection rules by country or region, but a simpler one is through the LocaBrowser website. Just enter the URL to test, the device, the browser and choose the country of the supposed visitor to test what happens. Really easy to set up.

The system of this web is not perfect, I tell you that, but “sometimes” it works. Above all, remember not to cache the URLs.

However, the best thing to do is to check it with real people from the countries for which you have created redirects, and I assure you that it works.

Nothing else, I hope I have helped you. As I said, I have been using this plugin for years with several clients and own websites and it works great.

How useful was this post?

Click on a smiley to rate it!

Average rating 5 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

1 thought on “How to redirect by country, region or IP in WordPress”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top