Rails maintenance page with images and styles

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: ,

Share if you like:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Technorati
  • Ma.gnolia
  • Reddit
  • description
Related posts:

Comments (10)

  1. Rafa wrote:

    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!

    Thursday, August 9, 2007 at 8:21 am #
  2. bjhess wrote:

    Rafa, I’ll give it a try. Thanks!

    Note: I edited your comment to “pre” the code.

    Thursday, August 9, 2007 at 9:55 am #
  3. Rafa wrote:

    I used it this evening and work fine :)

    Thanks for edit

    Thursday, August 9, 2007 at 12:12 pm #
  4. Barry wrote:

    Well, Rafa, I finally implemented this. Thanks for the tip - worked like a champ!

    Thursday, October 25, 2007 at 9:42 pm #
  5. Dean wrote:

    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?

    Tuesday, October 7, 2008 at 1:10 am #
  6. Barry wrote:

    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.

    Tuesday, October 7, 2008 at 4:59 pm #
  7. Dean wrote:

    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.)

    Tuesday, October 7, 2008 at 5:53 pm #
  8. Barry wrote:

    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.)

    Thursday, October 9, 2008 at 9:17 pm #
  9. Dean wrote:

    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.

    Thursday, October 9, 2008 at 10:03 pm #
  10. Barry wrote:

    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!

    Sunday, October 12, 2008 at 2:39 pm #