Can I check for maintenance mode before redirecting to subdomain?

I’m currently working on a site that will have several sub-domains that will need a re-direct based on country origin but the server doesn’t have the mod_geoip module allowed so I can’t use an .htaccess redirect, like:

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

# Canada
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://ca.abcd.com$1 [L]

I did some research and found that Cloudflare allows GEOlocation and I can use (code example):

$country_origin = strtolower($_SERVER['HTTP_CF_IPCOUNTRY']);
in a switch like (code is an example, hasn't been tested):

$country_origin = strtolower( $_SERVER[ "HTTP_CF_IPCOUNTRY" ] );
$clean_url = preg_replace("(^https?://)", "",  get_site_url() );
if ( !empty( $country_origin ) && $country_origin != 'us' ) {
  switch( $country_origin ) {
    case 'et';
        header( 'Location: http://' . $country_origin . '.' . $clean_url );
        break;          
    case 'us';
        header( 'Location: http://' . $country_origin . '.' . $clean_url );
        break;
    case 'ca';
        header( 'Location: http://' . $country_origin . '.' . $clean_url );
        break;
  }
}

Since each subdomain would have it’s own WordPress installation as the content would be re-written and managed separately how can I check if the site is enabled in maintenance mode to stop/prevent the redirect?

1 Answer
1

Each time that WordPress inserts a blog into maintenance mode, it creates a file in the root of your installation directory.

the file contains a string related to the plugin/theme/core that is being updated. Its name is .maintenance (doesn’t have a name, only extension).

The file stays there until the process is over, therefore you can check if the file exists by doing this:

if( file_exists(ABSPATH .'/.maintenance') ) { 
    // Stop redirection here
}

Which would be what you are looking for.

Leave a Comment