Automatically Redirecting Users to a Country-Specific Site

Written by elisabeth | Published 2022/05/23
Tech Story Tags: ip-geolocation | api | ip-api | ip-geolocation-api | http-redirect | website-development | redirecting-users | country-specific-site

TLDRUsers' needs have become more diverse, so if you have a website that is gaining global traction, you should offer them a personalized customer experience. To ensure they end up on the right website, you can use IP APIs to redirect them automatically. Translate IP addresses into countries and extract other useful information like time zones and currencies. Translate your website into different languages, show local store locations, and offer prices in local currencies. via the TL;DR App

While globalization is at full speed, there is a parallel development at pace. People crave content that is close to them. Users' needs have become more diverse, so if you have a website that is gaining global traction, you should offer them a personalized customer experience.

A great way to do so is by customizing your website for different regions, like countries or even cities. To ensure they end up on the right website, you can use IP APIs to redirect them automatically. Here is why and how you can do that.

Why Redirect Users?

User experience is essential if you want to be one step ahead of your competition. User experience can be broken down into four major principles: user centricity, accessibility, discoverability, and consistency. The design of your website or product is a significant factor for a great user experience, but personalization is equally essential.

Here are some of the ways you can customize your website to cater to the needs of your visitors:

  • Translate your website into different languages
  • Show local store locations
  • Offer prices in local currencies
  • Redirect to country-specific online shops
  • Customize your content by countries
  • Personalize your website, for example, with local holiday deals, country flags, etc.

How to Automatically Redirect Your Customers

The first step to offering country-specific websites or content is by getting an IP API. These will help geolocate your visitors via their IP addresses. With the help of an API, you can translate IP addresses into countries and cities and extract other useful information like time zones and currencies.

Next, you have to configure your website to determine the IP address of your users.

With the help of your IP APIs, these addresses will tell you your visitors' location. Once you are able to determine your visitor's area, implement a redirect code.

After you detect the user's country and/or language, you need to decide what you want to do next. For example, let's say we want to redirect the users to an EU-specific online store. So, if a visitor is located in the EU and goes to our website https://store.company.com, they will get redirected to https://blog.company.eu.

Some code for that in PHP would look like that (in my example, I used the IP API by ipbase.com):

$apikey = '[[ your api key ]]';
               $url = 'https://api.ipbase.com/v2/info?apikey=' . $apikey;

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

        $resp = curl_exec($curl);
        curl_close($curl);
        if(curl_getinfo($curl)['http_code'] === 200) {
            $data = json_decode($resp, true, JSON_THROW_ON_ERROR);

            if ($data['data']['location']['country']['is_in_european_union']) {
                header('Location: https://blog.company.eu');
                die();
            }
        }

        echo "NON EU SHOP";

Geotargeting can Help Your Conversions

Having multiple website versions for different locations and languages improves user experience and can even increase conversion rates, leading to more sales. That's why companies like Coca-Cola, Apple, and Microsoft also use geotargeting. Therefore, by customizing your website to your users' locations, your customers not only profit from a great UX, but you can also improve your business.


Written by elisabeth | Product Manager who loves writing about useful things that make our work life easier.
Published by HackerNoon on 2022/05/23