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:
- <?xml version="1.0"?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
- <type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">
- <arguments>
- <argument name="alternativeHeaders" xsi:type="array">
- <item name="x-client-ip" xsi:type="string">HTTP_X_REAL_IP</item>
- <item name="x-cf-connecting-ip" xsi:type="string">HTTP_CF_CONNECTING_IP</item>
- <!-- <item name="x-cf-mfcustom-ip" xsi:type="string">SOME_CUSTOM_SERVER_KEY</item> -->
- </argument>
- </arguments>
- </type>
- </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:
- <?php
- echo '<pre>';
- 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.