Geo IP Redirect and Cloudflare Integration Explained

How does the Geo IP Redirect module work with Cloudflare?

There is a known issue with using the Geo IP Redirect module with Cloudflare. It occurs due to the actual IP address of a customer being replaced by the proxy IP. To introduce compatibility between Cloudflare and the Geo IP Redirect module, follow the steps below.

Step 1. Create a new file in the root Magento directory:

app/etc/XIP/di.xml

Step 2. Paste the following lines of code to the newly created file:

  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  4.      <type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">
  5.          <arguments>
  6.             <argument name="alternativeHeaders" xsi:type="array">
  7.                 <item name="x-client-ip" xsi:type="string">HTTP_X_REAL_IP</item>
  8.                 <item name="x-cf-connecting-ip" xsi:type="string">HTTP_CF_CONNECTING_IP</item>
  9.                 <!-- <item name="x-cf-mfcustom-ip" xsi:type="string">SOME_CUSTOM_SERVER_KEY</item> -->
  10.             </argument>
  11.         </arguments>
  12.      </type>
  13. </config>

Step 3. Save the file and run the following CLI command from the root Magento folder:

php bin/magento setup:di:compile

If this doesn't work, then other values should be used instead of SOME_CUSTOM_SERVER_KEY, and the line should be uncommented. To get a proper value, create a test.php file in the root Magento folder and paste this code to it:

  1. <?php
  2. echo '<pre>';
  3. var_dump($_SERVER);

Run this script in the browser; to define the proper key to use, look out for your personal IP address in the right part of the output. Having found your IP address, check the left part of the corresponding line; that would be the key that stores the customer's IP. Copy the text in square brackets and use it instead of the SOME_CUSTOM_SERVER_KEY part in the di.xml file. 
      • Related Articles

      • Why Geo IP Redirect module does not work when using Amasty Service?

        In order for the module to redirect customers correctly, the IP database should be first imported in Stores → Configuration → Amasty Extensions → Geo IP Data. In case Amasty Service is used for IP Database Update Settings in Stores → Configuration → ...
      • Is the Geo IP Redirect module compatible with Varnish?

        Starting from version 1.7.0, the Geo IP Redirect extension does work with Varnish cache, but requires a few changes in the web server configuration for that. Please contact your hosting provider on this matter or follow the steps below. Step 1. Open ...
      • What is the difference between Geo IP Redirect and Store Switcher?

        Geo IP Redirect and Store Switcher are the two names of the same Amasty extension, they deliver identical functionality. Several product pages have been created to reveal the functionality of the module from different angles.
      • The module works inconsistently when tested with VPN. What could be the reason?

        Sometimes, the Geo IP Redirect module might not show expected behavior during testing. This is due to the fact that certain VPN extensions don't work correctly for some countries. There are two available options for thorough testing: 1. IP addresses ...
      • How to remove GET parameters from Redirect URLs?

        Since v.1.6.0 of the module, we have added the ability to exclude GET parameters from Redirect URLs. Please note that the Exclude GET Parameters from Redirect URL feature is available only as a part of an active product or support subscription. You ...