Maintenance mode on Apache / PHP using .htaccess and mod_rewrite

There always come a time where you need to temporarily stop a site for some required maintenance. The simplest quick&dirty method would be to stop the web server, but it’s not pretty for the users and also it brings down all the sites you are hosting. Another method (which I’ve used in the past) was to disable the virtual host config for that specific site in Apache, the traffic will then fallback to another default virtual host that served only error pages – it worked, it was quick and selective (doesn’t affect all the hosted websites).

But it still wasn’t perfect, and some complications arose when I switched to using SSL/HTTPS and traffic would not fallback correctly to the default virtual host.
Here’s the current method I am using, it’s fast to activate when neeed (one-liner on the server) and easy to implement.

First, you’ll need a .htaccess in your document root and mod_rewrite activated in Apache. The .htaccess would contains something like this:

And then you’ll have a file called maintenance.php in the document root:

You can activate maintenance mode by creating a file named “maintenance-mode-on” in the document root. It can be emtpy, it doesn’t matter.

You can then disable maintenance mode by deleting the file “maintenance-mode-on”.

Tip: I usually keep a text file called “maintenance-mode-off” in the document root with the above instructions so I’ll find it and know what to do.

Leave a Reply

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