Putting Your Site Down For Maintenance – Temporarily

February 25, 2015 6:59 pm Published by Leave your thoughts

I was recently asked if a site should be put in “maintenance mode” when it is being worked on – and if it is easy to do. The short answer is “yes” – the longer answer requires code. How exciting!

Whenever you are working on a site it is smart to put it in “maintenance mode.” The following is a very simple way that I use to do this. Please keep in mind this will only work for CMS type sites: WordPress, Joomla!, Drupal, your own fantastic CMS, etc. The great thing is, you do not need to load a plugin, or do any heavy-lifting when it comes to coding; unless you want to. This tidbit does require you be able to open/save your files and possibly write some html/css. But it is not necessary as you can copy and paste what you need from here.

First, we are going to add a simple ‘if’ statement to our index.php, or equivalent, file in the root directory of the site we are working on. At the very top – as in right after the opening <?php tag – add this:

// Put the site down for maintenance
if( file_exists( 'maintenance.php' ) )
include_once 'maintenance.php';

In this newly referenced ‘maintenance’ file we are going to store the code necessary to kindly tell our visitors that we are down for maintenance. You can make this as grand or plain as you want. This file is simply a big ‘if/else’ statement that will allow us to still see the site, but tell the rest of the world to come back later – without messing up the SEO!

Ok, here is the clever bit: we put what we want to display in the ‘else’ section – and it can be all the html/css/javascript/jquery you want. It is literally a full html webpage that is using a 503 header to tell the search engines ‘do not crawl me now, come back later’ and shows the rest of the world your message. Your ‘maintenance’ file will look like this:

  * Maintenance file
 // Allow website to be seen from certain ip addresses
 $allowed_ips = array('');
 if( in_array( $_SERVER['REMOTE_ADDR'], $allowed_ips ) ) {
 // Show me this site so I can do some work
 } else {
 // Tell the world we are down for maintenance
 header('HTTP/1.1 503 Service Temporarily Unavailable');
 header('Status: 503 Service Temporarily Unavailable');
 header('Retry-After: 3600');
 die('Your simple message here');

Let us address the $allowed_ips variable. This is where you put your ip address (just go to google.com and type in ‘what is my ip address’ – copy and paste that BETWEEN the ‘ marks). I have only put in a temporary “placeholder” (the – which is totally fake) as an example.

Next, let us address the ‘simple’ message to tell your visitors. In the die() statement, you can put in plain text or html-ized text. You could do something like:

 die('<h1>We are temporarily down for maintenance.</h1><h2>Sorry for the inconvenience.</h2><h3>Please check back in an hour.</h3>');

if you really wanted to. Or you can do a fancy type of webpage that requires writing html/css into this ‘maintenance’ file. I will leave the actual code to your imagination. Here is how you would do that. In between the last ‘header’ statement and the ‘die’ statement do this:

 header('Retry-After: 3600');
<!DOCTYPE html>
<html lang="en">
.... put your style stuff here, or inline with your elements ....
.... put your webpage stuff here ....
.... you can even add javascript here ....

With this simple little addition you can easy create your own ‘down for maintenance’ function to tell your visitors you are working and they should come back later – even all the SEO bots.

When you are all done just rename the ‘maintenance.php’ file, or delete it, or remove the code snippet from the ‘index.php’ file we referenced earlier.

Tags: , , , , , ,

Categorised in:

This post was written by dmbarber

Leave a Reply