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!