Recently I implemented a true maintenance page on Scrawlers using Mike Clark’s excellent HOWTO. Clark’s directions show you how to create a more dynamic maintenance page than the standard HTML version we all know and love. It’s a good technique.
With those clear directions, I was most of the way there. But I still wanted our maintenance page to make use of the existing logo and stylesheets we have already created for Scrawlers. Since I’m pretty much an Apache idiot, it took some time, but I finally came up with something workable.
The maintenance rewrite conditions and rules for Apache mod_rewrite are all over the web. I’ll reprint them here for posterity.
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
Unfortunately, these rules do not allow for proper loading of stylesheets and images on a maintenance page. I simply repeated the above conditions, and added a RewriteRule just for stylesheets and images. JavaScript came along, too.
# Check for maintenance file and allow images, styles and javascripts
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule "^/(images|stylesheets|javascripts)/?(.*)" "$0" [L]
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
Of course, the DRY piece of me believes there must be a way to eliminate those redundant conditions. Near as I can tell, rewrite conditions are only applicable for the rewrite rule directly following them. So when I tried to make use of a single set of conditions to impact both rules above, well, it just didn’t work. With any luck, someone will stop by this post and show us all an improvement to this Apache configuration file.
Technorati Tags: Ruby on Rails, Apache
Related posts:






Comments (10)
I use this code in my vhosts:
# Rewrite maintenance.html if file exists RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f RewriteCond %{SCRIPT_FILENAME} !maintenance.html RewriteCond %{SCRIPT_FILENAME} !^(.+).(gif|jpg|css|js)$ RewriteRule ^.*$ /system/maintenance.html [L]Regards!
Rafa, I’ll give it a try. Thanks!
Note: I edited your comment to “pre” the code.
I used it this evening and work fine :)
Thanks for edit
Well, Rafa, I finally implemented this. Thanks for the tip - worked like a champ!
The first example seems to work for me but when the maintenance.html file is not present, I get a 400 Error from Apache. The second example (in the comments) does not work at all and will not display any images…
Any suggestions?
Dean, is your dynamic maintenance page working apart from the stylesheet and image rewrite rules? I have Rafa’s rewrite rules in and it’s working swimmingly.
Barry,
I think my problem was with my rewrite rules that 1) checked for cached pages and 2) Sent all requests for images, css and javascript through apache. When I removed those rewrites, it worked perfectly. Anyone know how I can reintroduce those rewrites while having it work with the maintenance rewrites?
Here is the stuff I had that I removed:
RewriteRule ^(.*/)?.svn/ - [F,L] RewriteRule "^/images|stylesheets|javascripts/?(.*)" "$0" [L] RewriteRule ^/$ cache/index.html [QSA] RewriteRule ^([^.]+)$ cache/$1.html [QSA] RewriteCond %{REQUEST_FILENAME} !-f(ed - added formatting to code.)
Dean, did you try adjusting the order of your rules? If you move the maintenance page rules above those you’ve shared, they should be hit first when the maintenance page is in play and ignored otherwise.
(Note: I’m a bonehead when it comes to Apache.)
Barry, yeah, I tried rearranging the rules, no worky!
I am also an apache bonehead. I’m able to set up a virtual host and that’s about it. When it comes to rewrite, I usually google my solutions and see if they work. These two solutions just don’t seem to like each other.
Dean, you may want to ask in a forum like Rails Forum or perhaps the Rails IRC channel. Blog comments are a little too constrained to have a detailed conversation.
Hope you figure it out!