How to Set Up a "Site Down for Maintenance" Notice with Apache | WebReference

How to Set Up a "Site Down for Maintenance" Notice with Apache


By Sukrit Dhandhania

The Apache Web server is well equipped to run production level websites and web services. A production setup sometimes has scheduled downtimes. There are a number of ways to handle a maintenance downtime. You can either handle it inside of your web application by adding a page and directing all traffic to that page, or you can set up your web server in a sort of maintenance mode. In the maintenance mode, Apache will automatically direct all traffic to a maintenance page that you can set up. Let's take a look at how this can be set up.

Enable .htaccess

There are two ways to make changes to your Apache configuration - editing the Apache configuration directly, or using .htaccess files and storing the configuration along with the website's files. If you are hosted on a shared hosting platform, it is quite likely that the setup required to permit .htaccess files to function as desired has been done. If you are on a dedicated server, open the Apache configuration file httpd.conf in a text editor and look for a section containing the word AllowOverride. Make sure that this is set to allow all over rides by setting it to AllowOverride All. Now you should be able to use .htaccess files.

Create the Maintenance page

Next up you need to create a maintenance page that you will direct all the traffic to during maintenance periods. You can make it so that it simply says "Site Down for Maintenance. We'll be Back Soon." or you can create something more elaborate with images. I'll leave the decision on how you want to create this page to you. We can proceed with the assumption that a page called maintenance.html has been created with the maintenance notice.

Enable Mod Rewrite

We have covered the setup and usage of the Apache module mod_rewrite in a previous article. Use the article to set up mod_rewrite. If you are not certain if mod_rewrite has been set up on your server, you can use an example from the linked article to test your setup. Note that the maintenance page redirection will not work correctly without a functional mod_rewrite setup.

Creating the .htaccess File

You can now set up the forwarding to the maintenance page. Create a file with the name .htaccess in the root directly of your website or web application. Enter the following bit of code into the file. Replace maintenance.html with the name of your maintenance file.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteRule $ /maintenance.html [R=302,L] 

Testing the Setup

Now that your setup is in place it's time to run a test to make sure things are working as desired. Fire up your favorite web browser, clear the browser's cache and history files, and then hit the URL of the site you just set to maintenance mode. You should be automatically redirected to the maintenance page. If not, revisit the set up instructions.

Getting out of Maintenance

When you are done with your site upgrade or maintenance work you should remember to roll back the changes we made here. For that all you need to do is to remove the code we added to the .htaccess file. Remove the code and test it in your web browser after clearing the cache files again.

IP Exceptions

If you are running an upgrade on your website, you will need to be able to look at and test out the site. There is a way by which you can post the maintenance notice for anyone hitting your websites but allow you to view the site as you would normally. For this, you will require a static public IP address. Add the following line to your .htaccess configuration:

RewriteCond %{REMOTE_HOST} !^117\.201\.1\.154

Replace 117\.201\.1\.154 with your IP address. Your code will now look something like this:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_HOST} !^117\.201\.1\.154
RewriteRule $ /maintenance.html [R=302,L] 

Now launch the site in your web browser and you should see the site normally, while everyone else will be redirected to the maintenance page. Do remember to remove these settings once you are done with your maintenance work or upgrades.

Original: August 24, 2009