Occasionally I need to take my sites down for a regular maintenance and I’ve come up with a pretty neat way of setting up a?website maintenance page?using Apache mod rewrites. Once the rewrites are in place, you don’t need to restart your Apache webserver for any subsequent maintenance periods. ?The idea is to simply drop an empty file into your webroot and your maintenance page will be up.
So here’s the rewrite code:
RewriteEngine On RewriteCond %{REMOTE_ADDR} !your-ip-address RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f RewriteCond %{SCRIPT_FILENAME} !maintenance.html RewriteCond %{REQUEST_URI} !^/?(maintenance)/ RewriteRule ^.*$ /maintenance.html [R=503,L] ErrorDocument 503 /maintenance.html
Now let me explain the rewrite rules line by line:
- Turn on the rewrite engine
- Allow your IP address to hit the site directly bypassing the maintenance page. This is useful for testing, but you don’t need to do this step.
- Ensure maintenance.html file exists
- Ensure maintenance.enable file exists. This is how you turn on/off your maintenance page without restarting Apache!
- Apply this rule to all requests, unless maintenance.html page is requested. This is important to avoid applying the rule in a loop.
- Don’t apply rewrite rules to /maintenance subdirectory. This is useful if your maintenance page consists of more than one file. I.e you have a fancy maintenance page with js and css files that you might want to place in a maintenance subdirectory.
- Finally the rule itself.
That’s it. It’s a simple process, but a very useful one if maintaining a good online presence is important to you.
Cheers
Marko