Drupal: How to detect country and redirect to country specific website by using Cloudflare

May 13, 2020

Introduction

Assume you have a drupal website and using cloudflare. You are having an Amazon affiliate drupal content type, and you are collecting ASIN code of two countries:

  • India
  • USA

Now you want to display only one link of amazon which will redirect to country specific website. i.e. either amazon.com or amazon.in Lets see how you can do it in Drupal.

I did this code because I have an amazon affiliate website, and I want to redirect in respective country. As, there is no point of sending an American to amazon.in

Drupal Modules

There are drupal modules available which can help you detecting country where your website is opening. Some of good modules are:

The problem with these module is that they will ask you to download big databases and then query from that. And, manytimes they say that the IP is not associated with any country. And, they are a bit complex to setup.

I did not use them at all.

Cloudflare

Cloudflare is an excellent CDN, which gives an awesome free plan. When you connect your website through cloudflare. Cloudflare will send some header information to your server. See cloudflare article for http headers, what they send.

To detect country, they send a header name: CF-IPCountry.

# if your website is opening in USA, the header value is:
CF-IPCountry: US

# for india
CF-IPCountry: IN

I will use this information to redirect to country specific site.

Drupal Controller

For this, I defined a controller in my custom module. Note: With Controllers you can define an URL which acts as orchestrator to your controller code.

I will be doing following steps:

  1. Define a Controller in your custom module
  2. For this controller, define routing rules
  3. Most important: Disable caching for this controller

1. Define a controller

In your module base directory, create a file in src/Controller/AmazonLinkController.php

<?php
namespace Drupal\gyanblog\Controller;

use Drupal\Core\Controller\ControllerBase;

class AmazonLinkController extends ControllerBase {

    public function redirectToAmazon() {
        $india_code = "your affiliate code for india";
        $usa_code = "your affiliate code for usa";

        $india_asin = \Drupal::request()->get('i');
        $usa_asin = \Drupal::request()->get('u');
        $product_title = \Drupal::request()->get('t');
        
        $country_code = $_SERVER['HTTP_CF_IPCOUNTRY'];

        $amazon_link = "";
        if ($country_code === 'IN') {
            $amazon_link = "https://www.amazon.in/dp/" . $india_asin . "/?tag=" . $india_code;
        }
        else if ($country_code === 'US') {
            if (isset($usa_asin)) {
                $amazon_link = "https://www.amazon.com/dp/" . $usa_asin . "/?tag=" . $usa_code;
            }
            else {
                $amazon_link = "https://www.amazon.com/s/?k=" . $product_title . "&tag=" . $usa_code;
            }
        }
        else {
            $amazon_link = "https://www.amazon.com/s/?k=" . $product_title . "&tag=" . $usa_code;
        }

        return new \Drupal\Core\Routing\TrustedRedirectResponse($amazon_link, 302);
    }
}

There are some amazon specific code which opens up a search box if you are not passing any ASIN code of product. Lets leave that details in this article. In above code, I’m expecting few query parameters:

  • param: i, For taking ASIN code of India products
  • param: u, For taking ASIN code of USA products
  • param: t, For taking title of product in case I want to show search box in amazon.com

2. Define Routing Rules

Lets say your module name is: gyanblog. Create a new file named: gyanblog.routing.yml, or edit if you already have such file.

gyanblog_amazon_link.controller:
  path: '/recommend/amazon'
  defaults:
    _title: 'Amazon Buying Guide'
    _controller: '\Drupal\gyanblog\Controller\AmazonLinkController::redirectToAmazon'
  requirements:
   _permission: 'access content'

In above yaml file, we defined a route url, and a handler function for that url. Permission is defined for all people having access content permission.

When you save this, and redirect users to this URL: /recomment/amazon, you will see that user will be redirected to always one URL. This is the problem caused due to caching.

3. Disable caching

Lets edit our routing file

gyanblog_amazon_link.controller:
  path: '/recommend/amazon'
  defaults:
    _title: 'Amazon Buying Guide'
    _controller: '\Drupal\gyanblog\Controller\AmazonLinkController::redirectToAmazon'
  requirements:
   _permission: 'access content'
  options:
    no_cache: 'TRUE'

Note the no_cache option above. This will configure no caching for this page. Which is what we require.

Final Render

After saving this, goto your twig file. And, just put an a href tag, with target as _blank

{% raw %}
{% set product_title = "#{paragraph.getParentEntity().field_product_title.value}" %}

{% set india_asin = content.field_india_asin[0] | render|striptags|trim %}
{% if content.field_usa_asin[0] %}
    {% set usa_asin = content.field_usa_asin[0] | render|striptags|trim %}
{% endif %}

{% set options = "/recommend/amazon?i=" ~ (india_asin) ~ "&t=" ~ (encoded_product_title) %}
{% if content.field_usa_asin[0] %}
    {% set options = "/recommend/amazon?i=" ~ (india_asin) ~ "&u=" ~ (usa_asin) ~ "&t=" ~ (encoded_product_title) %}
{% endif %}

{# render link #}
<a class="btn btn-primary btn-lg active" href={{ options }} role="button" target="_blank">Buy on Amazon</a>
{% endraw %}

Final words

The above post focuses on how to detect country of user, and do something country specific. Other code assumes, you have certain knowledge of drupal code.

Let me know if you have some comments or query in comments box.


Similar Posts

Latest Posts