drupal|May 13, 2020|4 min read

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

TL;DR

Read the CF-IPCountry header set by Cloudflare to detect the visitor's country and dynamically render country-specific Amazon affiliate links in Drupal.

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

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\gyanbyte\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: gyanbyte. Create a new file named: gyanbyte.routing.yml, or edit if you already have such file.

gyanbyte_amazon_link.controller:
  path: '/recommend/amazon'
  defaults:
    _title: 'Amazon Buying Guide'
    _controller: '\Drupal\gyanbyte\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

gyanbyte_amazon_link.controller:
  path: '/recommend/amazon'
  defaults:
    _title: 'Amazon Buying Guide'
    _controller: '\Drupal\gyanbyte\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.

Related Posts

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal - How to rename column of a content type

Drupal - How to rename column of a content type

Introduction You already have a content type with one or more fields in it…

Drupal 8 - How to hide help link About text formats and text format guidelines

Drupal 8 - How to hide help link About text formats and text format guidelines

Problem In drupal textarea field, it was always a pain to see the two links…

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Problem Statement I’ve been using image styles, and heavily used “Scale and crop…

Drupal 8 Comment module - How to configure comments module from ugly to beautiful - Theming comments module

Drupal 8 Comment module - How to configure comments module from ugly to beautiful - Theming comments module

Introduction Drupal provides a powerful comment module, which comes as a part of…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…