GeoIP redirects with WP Engine

Entry no. 5|web development |

Where I work we host all of our websites with WP Engine. They offer compelling hosting plans that do a lot of things right and at a good price. One of the main reasons I wanted to host our websites with them was because they manage the hosting. As in I don’t need to update Wordpress when there is a new release, WP Engine does. On top of that they offer finely tuned servers, CDN’s, staging sites, SSL certificates through Let’s Encrypt and 24/7 support. All of which is great!

This takes a huge amount of weight off my shoulders, as the lone creative managing all of our organizations websites and peace of mind for the business units within the organization. Should anything go wrong — like me getting hit by a bus — then they have 24/7 support with WP Engine.

One of our business units had a requirement to redirect all visitors from the United States to a specific page on their website. Here the visitor would be presented with some information about the current state of the product in the US and a form to register their interest. This was due to the product being under FDA review and what claims were allowed to be made within the US.

To handle this I implemented WP Engines GEOIP plugin (note: this will only work on the Business or higher plan) along with the below PHP function.

/** USA GEOIP REDIRECT but allow bots **/
function country_geo_redirect() {
  $country = getenv('HTTP_GEOIP_COUNTRY_CODE');
  $agent = $_SERVER['HTTP_USER_AGENT'];
  if ( $host == '' || $host == '' || isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']) || $country != "US" ) {
  wp_redirect('', 301);
add_action('init', 'country_geo_redirect');

What the above function does is checks if the visitor has requested certain pages which we don’t want to restrict access to — in this case, our US landing page and the RSS feed. Next we check if they are from outside the US. Finally we check if they are a search bot — because we still want Google and the like to access our site so they can index. If any of those conditions return true then the visitor is allowed to access the requested page.

However if those conditions are not met, then the visitor is redirected to the US landing page. This is initialized as soon as possible from within Wordpress so the visitor doesn’t need to download additional resources.

Result achieved. We are now redirecting US based visitors to a “register your interest” page during the time the product is under going FDA review, while still allowing Google and other search bots to index our site.

Some side notes — The main reason for going with WP Engines GEOIP plugin over other Wordpress GEOIP plugins, is that other plugins would cause a redirect loop. I suspect the redirect loops occurred because of WP Engines caching technology. Also initially I had blocked US visitors from viewing the RSS feed. However this broke functionality with MailChimp — who are US based — which would scrape our RSS feed for new blog posts and send out a monthly news digest.