Verse Of The Day

Friday, February 23, 2007

Apache "Maintenance" Pages Using mod_rewrite

Just found out about the power of mod_rewrite in the Apache web server. To enable a system wide "maintenance" page (system not available type thing) you need enter only 2 lines of code in the httpd.conf file and restart apache. This is so simple, it kind of baffles me why this example wasn't mentioned in the 2 links I used to figure it out.

The pattern matches for everything except maintenance.html and sends the request to maintenance.html. That is important, because at first I tried to redirect all requests by matching on ^/(.*) but that got into an endless loop, since it would redirect request to maint page which redirected to the maint page which redirected to, well you get the idea.

Here is my rule:
RewriteEngine On
#RewriteRule !^/maintenance.html$ /maintenance.html [R,L]

Save file, cd ../bin and sudo ./apachectl restart and presto! All requests go to maintenance.html instead of the intended destination. Comment out those 2 lines, restart, and all normal.

Links of interest:

My maintenance redirect wasn't working for requests already coming in over SSL. I had to add the rule to the secure virtual host config section as well for it to work. Special thanks to "shimmyshack" on the alt.apache.configuration newsgroup for pointing me in the right direction.


estorino said...

You can do all this without having to restart apache, just insert a condition before your rewriterule, like:
RewriteCond /var/www/maintenance.html -f
RewriteRule ^.*$ /var/www.maintenance.html

od said...

Hi ,

I have kept my missing.html in htdocs. Now this page contains a logo which is not getting displayed in the maintainenece page. The access logs show a 304 for the image file.

"GET /missing.html HTTP/1.1" 200 3378 - - [04/Dec/2012:10:02:26 +0000] "GET /BT_45pix_pos.gif HTTP/1.1" 304 -

Please help