• I am trying to implement a sitewide redirect to a subdomain based on the user’s country.

    I found a sample code showing how to do this, and have modified it for my domain and placed it at the end of my theme’s functions.php file (before the “?>”) but I seem to be stuck in a redirect loop. This is the error I see in the error log when I use the code below:

    PHP Parse error: syntax error, unexpected ‘;’ in /nas/content/live/petersengames3/wp-content/themes/seohub/functions.php on line 212

    Here are the instructions I found elsewhere on this forum:

    Here’s a code snippet for you to try out, drop this into your functions.php file after you customize the redirects to your domains and respective countries. We’ve seen this work well with other GeoIP Users:

    function country_geo_redirect() {
    $country = getenv(‘HTTP_GEOIP_COUNTRY_CODE’);
    if ( $country == “US” ) {
    wp_redirect( ‘https://us.domain.com’;, 301 );
    exit;
    } else if ( $country == “FR” ) {
    wp_redirect( ‘https://fr.domain.com’;, 301 );
    exit;
    }
    }
    add_action(‘init’, country_geo_redirect’);

Viewing 9 replies - 1 through 9 (of 9 total)
  • Alright lets take a look ??

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

    A few things to be careful of when copying and pasting

    1. Watch the quotes “FR” vs “FR” – smart quotes will give you problems, straight quote marks is what you’re after
    2. The formatting of what you had was all off. You had semicolons (;) where you didn’t need and was missing single quote marks (‘)
    3. I would stick this function at the top of functions.php due to the importance of the function

    I’ve used this GeoIP functionality a bit, so if you have anymore questions just ask.

    Goodluck!

    Is there a way to spoof to check the redirect is working? I’ve tried adding the /?geoip&region=FR to the end of the URL in my browser bar but it doesn’t seem to work. Thanks much!

    One sure fire way is to use a VPN. I use Mullvad for testing my GeoIP redirects, then I can place myself anywhere in the world and test until my heart is content ??

    Thanks, Rhys, I hadn’t thought of that but makes perfect sense. Cheers!

    Thread Starter faeryrose

    (@faeryrose)

    Thank you so much for your reply!

    Sorry – I copied and pasted that code directly from another forum post, and didn’t use the insert code option. I really should have been more thorough – especially when asking others to take the time to help me fix it!

    When I was trying to make this work initially, I had the same thought about the formatting, and tried typing it in from scratch… I did put in the missing single quote in the “add action” line, and removed the semicolons after the domain names and before the commas…but I was never able to make it work. I didn’t think of moving it to the top of the file…

    I ended up getting some help from another developer, who couldn’t figure it out either…but this is what we came up with instead, and it seems to work beautifully (and doesn’t lock me out of the admin side of each domain)

    //REDIRECT CODE 
    
    function country_geo_redirect() {
    	$geo = WPEngine\GeoIp::instance();
    	if ( !current_user_can('administrator') ) {
    	switch ($geo->geos['countrycode']) {
    		case "FR":
    		case "GB":
    		case "DE":
    			if ($_SERVER['HTTP_HOST'] != 'eu.example.com') {
    		  	wp_redirect( 'https://eu.example.com', 301 );
    				exit;
    			}
    			break;
    		case "BN":
    		case "CN":
    		case "JP":
    			if ($_SERVER['HTTP_HOST'] != 'as.example.com') {
    		  	wp_redirect( 'https://as.example.com', 301 );
    				exit;
    			}
    			break;
    		case "AU":
    		case "NZ":
    			if ($_SERVER['HTTP_HOST'] != 'au.example.com') {
    		  	wp_redirect( 'https://au.example.com', 301 );
    				exit;
    			}
    			break;
    		default:
    			if ($_SERVER['HTTP_HOST'] != 'example.com') {
    				wp_redirect( 'https://example.com', 301);
    				exit;
    			}
    	}
    	
    }
    }
    add_action('init', 'country_geo_redirect');

    Also – this may help someone else – but I had to make sure all CDN and Caching was turned off, as that was preventing the changes in the code to “work” for us…

    We couldn’t get the spoof code to work either. Well, it was working a little – using example.com/?geoip&country=FR would redirect to “eu.example.com”, but then it would drop the ?geoip&country=FR part of the URL, and then redirect a second time back to example.com. The if ($_SERVER['HTTP_HOST'] != 'eu.example.com') part of the code seemed to fix this – we could then spoof by going to fr.example.com/?geoip&country=FR, and since we were already at the “correct” url, it wouldn’t “redirect” us anywhere…

    And wrapping the whole switch statement in the if ( !current_user_can('administrator') ) meant I was no longer redirected away from each sub-site even when logged in. (This is a multisite install using subdomains).

    Thank you again for your advice and assistance – and just for simply taking the time to help! I really appreciate it!

    No worries, usually the simplest solution is the hardest to come by ??

    Hey all, what ended up working for us was the following. It’s included as the first function in our functions.php file immediately below the <?php:

    function country_geo_redirect() {
      $geo = WPEngine\GeoIp::instance();
        if ( 'UK' === $geo->country() ) {
    		wp_redirect( 'https://example.com', 301 );
    		exit;
        } 
      }
    add_action('init', 'country_geo_redirect');

    Note that BEFORE you even add this to your functions.php, you will want to make sure the GeoIp plugin is installed, activated, and that *at least* the country cache bucket is enabled by the fine folks at WPEngine. Of course, you could probably easily rewrite the function to check for the instance, first, but this was a simple, working approach for now.

    And, of course, UK is just one possible country code. I found a list of country codes here: https://jpgamboa.com/country-codes-for-wp-engine-geoip/

    Awesome, glad to see you all got there in the end. Yeah should have mentioned that you need to be on the business plan or above, need the plugin installed and talk to WPEngine support. That is all listed under the FAQ for this plugin.

    Hello, everyone! I know that this topic has been successfully resolved in a couple different ways above, but I wanted to connect with you because I have run into the following issue:

    I’ve worked through the solutions in Faeryrose’s, Rhys’s, and Matthew’s posts above, and have been receiving the same error. When testing using /?geoip&country=UK to spoof the request, the redirect works fine. However, when testing from the countries themselves (either in-country or VPN), visitors receive a redirect loop error.

    I’ve included my most recent code attempt below, although it was pulled directly from @faeryrose above (just with different domains based on our site’s construction). Any help or suggestions will be greatly appreciated because we are going crazy trying to figure out if it is related to the code or to some other aspect of our installation.

    Thank you!

    function country_geo_redirect() {
    	$geo = WPEngine\GeoIp::instance();
    	if ( !current_user_can('administrator') ) {
    	switch ($geo->geos['countrycode']) {
    		case "UK":
    			if ($_SERVER['HTTP_HOST'] != 'mysite.com/uk/') {
    		  	wp_redirect( 'https://mysite.com/uk/', 301 );
    				exit;
    			}
    			break;
    		case "CA":
    			if ($_SERVER['HTTP_HOST'] != 'mysite.com/ca/') {
    		  	wp_redirect( 'https://mysite.com/ca/', 301 );
    				exit;
    			}
    			break;
    		default:
    			if ($_SERVER['HTTP_HOST'] != 'mysite.com') {
    				wp_redirect( 'https://mysite.com', 301);
    				exit;
    			}
    	}
    	
    }
    }
    add_action('init', 'country_geo_redirect');
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Global Redirect stuck in loop’ is closed to new replies.