A different Maintenance Mode using .htaccess

Sometimes you need to test your site after an upgrade without the general public having access

  • i.e. you changed loads of code in your child theme
  • you have made changes to the database

You simply want access to the site while upgrading plugins/code while others can’t.
This can be achieved by using

  • .htaccess
  • a second (multi) site/domain to handle the temporary redirection

You .htaccess should have the following:


# we need this
RewriteEngine On
RewriteBase /
#####################################################
# MAINTENANCE MODE
# This is the best way to make maintenance mode:
# - exclude every address **BUT** our addresses
# - send non YOUR_DOMAIN addresses to 302 page
# - let our addresses go through
#####################################################
# an ip address range
RewriteCond %{REMOTE_ADDR} !^192\.168\.101 [NC]
# an ip address
RewriteCond %{REMOTE_ADDR} !^192\.168\.200\.15 [NC]
# another ip address range
RewriteCond %{REMOTE_ADDR} !^XXX\.YYY\.ZZZ [NC]
RewriteCond %{REMOTE_HOST} !^.*YOUR\.DOMAIN\.COM [NC]
RewriteCond %{REMOTE_HOST} !^.*YOUROTHER\.DOMAIN\.com [NC]
# Since the redirection **MIGHT** be altered on occasion, the client SHOULD continue to use the Request-URI for future requests.
RewriteRule ^(.*)$ http://TEMP_DOMAIN/maintenance-for-other-domain.html [R=302,L]

Your maintenance-for-other-domain.html file should contain at the very least:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="refresh" content="60; URL=http://www.salesessentials.com/">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<head>
<title>Salesessentials Maintenance Mode</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body style="margin:0;padding:0;background:white;">
<div style="width:99%;height:99%;margin:0;padding:0;background:white;">
<img src="salesessentials-maintenance-image.png" style="position:absolute;margin:auto;top:0;left:0;right:0;bottom:0" />
</div>
</body>
</html>

 

How does this work?

When you come along from any of the domains you specified in the .htaccess file you are excluded from the rewrite rule.

If anyone from an ip-address or host not specified in the .htaccess file they are not excluded and the rewrite rule is applied.

What the lines mean: “If you are not from any of the provided ip address or host/domain names the rewrite rule is applied”.

The rewrite rule applies a TEMPORARILY (302) redirection to your other side.

That file will display a maintenance image. The file, though, is reloaded every 60 seconds and re-directed to go to the main site.
If the .htacess file is still in action, the maintenance image will be displayed again, the file reloaded after 60 seconds.

This will only stop if you comment out the rewrite rule in the .htaccess file.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

You must tick the checkbox for 'I am not a robot' before you can submit your comment!