How does the GeoIP Redirect module work with Cloudflare?

How does the GeoIP Redirect module work with Cloudflare?

There is a known issue with using the GeoIP 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 GeoIP 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

      • Is the GeoIP Redirect module compatible with Varnish?

        Starting from version 1.7.0, the GeoIP 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 ...
      • Why GeoIP 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 → GeoIP Data. In case Amasty Service is used for IP Database Update Settings in Stores → Configuration → ...
      • What is the difference between GeoIP Redirect and Store Switcher?

        GeoIP 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 GeoIP 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 ...
      • Why doesn't redirect function on the frontend?

        This guide outlines key areas to examine to ensure your redirects operate smoothly. 1. Verify Redirects with GET Parameters Firstly, it is essential to verify whether redirects function when using a GET parameter. This check helps to rule out the ...