GeoIP Redirect Only Working When Spoofed
-
Hello!
I have added the GeoTarget plugin to my site using the suggestions and options others have used in this forum. The goal is to do a full site redirect to the corresponding site based on the visitor’s country. I’ve connected with WP Engine and they checked the setup of the buckets and they let me know that everything in the back end is correct.
Our challenge is that visiting the site using tools like GeoPeeker or ShotSherpa displays the primary, US site, regardless of the selected countries. However, when we spoof the country in the address bar — using yourdomain.com/?geoip&country=AU — we are successfully redirected to the correct URL.
I’m posting the function we’ve added to the top of the functions.php file, in case there is something in the code itself that is causing the redirection / IP recognition to fail.
Any assistance or resources you can provide is greatly appreciated. Please also let me know if I need to clarify / add any information.
function country_geo_redirect() { $geo = WPEngine\GeoIp::instance(); if ( 'UK' === $geo->country() ) { wp_redirect( 'https://mysite.com/uk/', 301 ); exit; } else if ( 'CA' === $geo->country() ) { wp_redirect( 'https://mysite.com/ca/', 301 ); exit; } else if ( 'IE' === $geo->country() ) { wp_redirect( 'https://mysite.com/ie/', 301 ); exit; } else if ( 'AU' === $geo->country() ) { wp_redirect( 'https://mysite.com/au/', 301 ); exit; } } add_action('init', 'country_geo_redirect');
- The topic ‘GeoIP Redirect Only Working When Spoofed’ is closed to new replies.