Use a 503 for your Rails maintenance page

Most of the docs out there about how to configure Apache for Capistrano’s deploy:web:disable task use a quick RewriteRule to the maintenance page.

That looks OK, but it doesn’t change the status code of the response: clients will usually get a 200 OK, indicating that your server is fine and that you’re sending the normal response. The correct status code to respond with is 503 Service Unavailable. With a 503, you’ll hopefully prevent search engines from indexing your maintenance page, make life easier on API consumers, enable AJAX requests to correctly deal with the site being down, and so on.

Here’s the configuration I’m using now:

1
2
3
4
5
6
7
# Show maintenance page if it exists
ErrorDocument 503 /system/maintenance.html
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png)$
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$  -  [redirect=503,last]

About that suspicious looking redirect=503 flag, here’s an excerpt from the flags section of the mod_rewrite docs:

While this is typically used for redirects, any valid status code can be given here. If the status code is outside the redirect range (300-399), then the Substitution string is dropped and rewriting is stopped as if the L flag was used.

So technically speaking, you can give RewriteRule anything for that second argument, and the last flag doesn’t need to be specified because it’s automatically applied. I just find it more intention-revealing to use the path “-” and stick the last flag on there anyway.

Comments

  1. Karl VargaAugust 17, 2009 @ 07:50 PM

    Thanks, just what I was looking for!

    I have a couple extra lines in mine to allow robots access to the sitemap and robots files even when the site is down. I was a little disconcerted when I saw that according to Google Webmaster the contents of my robots file was my maintenance page :/ Ditto for the sitemap.

    RewriteCond %{REQUEST_URI} !^/robots.txt RewriteCond %{REQUEST_URI} !^/sitema p

  2. Marcelo SilveiraFebruary 22, 2010 @ 04:02 AM

    Hey thanks. Handy.

  3. Akhil BansalJune 10, 2010 @ 03:46 AM

    Thanks, Using your tips in current deployment. ;-)

Post a comment

Comment
(not published)
(optional)