• Resolved Chet

    (@chethardin)


    Hey –

    I am using this function, and I am receiving a redirect loop. Any ideas?

    
    function country_geo_redirect() {
    $country = getenv('HTTP_GEOIP_REGION');
    if ( ( $country == "MO" ) && ! is_user_logged_in() ) {
    	wp_redirect( home_url( '/missouri-page' ) ), 301 );
    exit;
    } 
    }
    add_action('init', 'country_geo_redirect' );
    
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Taylor McCaslin

    (@taylor4484)

    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" );
    
    Plugin Contributor Taylor McCaslin

    (@taylor4484)

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirect loop’ is closed to new replies.