Howdy Chet,
You have to make sure you don’t just keep redirecting once you get to the desired page, something like this should work:
function country_geo_redirect() {
$region = getenv("HTTP_GEOIP_REGION");
if ( '/missouri-page' != str_replace( home_url(), '', get_permalink() ) || ( 'MO' != $region ) || is_user_logged_in() ) {
return;
}
wp_redirect( home_url( "/missouri-page/" ) ), 301 );
exit;
}
add_action("init", "country_geo_redirect" );
Thread Starter
Chet
(@chethardin)
Hey Taylor –
Thanks! Can I ask a quick follow-up?
I am redirecting people in Missouri to a sub-site on a multisite installation. I also want to not redirect them for one page. Would this work?
function region_geo_redirect() {
$region = getenv("HTTP_GEOIP_REGION");
if ( '/sub-site-url' != str_replace( home_url(), '', get_permalink() ) || '/page-on-main-site' != str_replace( home_url(), '', get_permalink() ) || ( 'MO' != $region ) || is_user_logged_in() ) {
return;
}
wp_redirect( home_url( "/sub-site-url" ) ), 301 );
exit;
}
add_action("init", "region_geo_redirect" );
Yep that looks about right. We are contemplating how to build in a helper function in future releases to make this kind of redirect easier to setup.