• Hi guys,
    I’m trying to get a redirect to work which redirects all visitors from the US to a specific page. But everytime I enable the function the site throws a generic 500 error.

    The code in question is:

    function country_geo_redirect() {
      $country = null;
    
      if ( class_exists( 'WPEngine\GeoIp' ) ) {
      ??$geo = WPEngine\GeoIp::instance();
      ??$country = $geo->country();
      }
    
      if ( $country == "US" ) {
        wp_redirect( 'https://domain.com/us', 301 );
        exit;
      }
    }
    add_action('init', __NAMESPACE__ . '\\country_geo_redirect');

    The namespacing in the add_action bit is because I’m using the Sage starter theme. Any ideas of whats going on?

Viewing 1 replies (of 1 total)
  • Thread Starter Rhys

    (@rjdusk)

    So I’m getting a little closer

    function country_geo_redirect() {
      $country = getenv('HTTP_GEOIP_COUNTRY_CODE');
      if ( $country == "US" && !is_page( 'us' ) ) {
        wp_redirect( 'https://domain.com/us', 301 );
        exit;
      }
    }
    add_action('init', __NAMESPACE__ . '\\country_geo_redirect');

    The problem I’m having now is when the redirect happens I end up in a redirect loop, though that’s exactly what I’m trying to prevent with the

    && !is_page( 'us' )

    piece in the if statement

Viewing 1 replies (of 1 total)
  • The topic ‘US based redirect resulting in 500 error’ is closed to new replies.