Support » Plugin: WP Engine GeoTarget » 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');
Viewing 3 replies - 1 through 3 (of 3 total)
  • brianfohn

    (@brianfohn)

    WPEngine Employee

    Hi philmccollam,
    Thanks for reaching out!

    If the goal is to redirect based on Country Code using GeoTarget, you should ensure that the HTTP_GEOIP_COUNTRY_CODE environment variable is being properly utilized in your PHP code.

    Here’s an example function from our documentation that could be used as a model to help you reach your goal:

    function country_geo_redirect() {
    $country = getenv('HTTP_GEOIP_COUNTRY_CODE');
    if ( $country == "US" ) {
    wp_redirect( 'http://us.domain.com';, 301 );
    exit; }
    else if ( $country == "FR" ) {
    wp_redirect( 'http://fr.domain.com';, 301 );
    exit; }
    }
    add_action('init', country_geo_redirect');
    Thread Starter philmccollam

    (@philmccollam)

    Thanks, Brian! I apologize for being so late to this response.

    We were able to get the redirect set up, but every solution that seemed to be working when spoofing through /?geoip&country=XX while visiting in the US ended up throwing the site into redirect loops when visiting from the countries themselves.

    Our install is utilizing 301 redirects to drive traffic from the site’s old URLs — https://XX.mysite.com — to the new URLs — https://mysite.com/XX. And we are also directing visitors from HTTP to the HTTPS domains. Is it possible that any of these redirections are causing the GeoIP loops?

    brianfohn

    (@brianfohn)

    WPEngine Employee

    Hey philmccollam,

    I’m glad to hear that you’re making progress with you code.
    With regards to troubleshooting the redirect loops you are seeing, one way to isolate whether the domain redirect and http => https redirects are affecting this would be to test this behavior with each of those redirects disabled.
    I hope this helps point you in the right direction.

    I also want to mention that if you aren’t already working with a development service or an agency, you may want to look into using Codeable’s services, as they have many PHP and WordPress developers who may be able to assist with your project directly.
    Also depending on the scope and scale of your project, our network of consultants could be an ideal resource as well.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘GeoIP Redirect Only Working When Spoofed’ is closed to new replies.