Hosting a Downtime Page with Apache

I had to help set up a downtime page for a site experiencing an extended outage. We wanted a “We’re working on it!” type page to let people know what it would be back up soon. But the problem is that if you serve this as a static HTML page, it would (a) only work on the site’s root page, and (b) be served with an HTTP 200 status code, potentially getting crawled by search engines and ending up in peoples’ caches. Both of those are bad.

What I really wanted was to set an HTTP status code that indicated that there was an error condition, so that browsers would hopefully not cache the page, and so that search engines would know not to index the page contents. HTTP 503 works perfectly for this — “Service Unavailable.”

I ended up setting up a new Apache virtual host on my existing server (not the one that’s down, of course), creating a quick page named downtime.html (the only content), and throwing together the .htaccess (assembled piecemeal from some reference sites, admittedly):

RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteRule !^/downtime.html$ /downtime.html [L,R=503]
ErrorDocument 503 /downtime.html

The result is that all pages on the site now get the downtime page in response, served with the appropriate HTTP status code. Ta-da! This isn’t anything terribly involved, of course, but figuring out how to set a 503 via mod_rewrite was quite confounding.

Presence in Rails

One thing that might not be immediately obvious is that an empty string in a conditional in Ruby will return true. See for yourself:

irb(main):006:0> puts "Whoa" if ""
(irb):6: warning: string literal in condition
Whoa

The same holds true for other empty things — say, arrays:

irb(main):008:0> puts "See?" if []
See?
=> nil

Pure Ruby will let you call .empty? (on some methods — it’s not standard on Object though!), but code like this is silly and it’s easy to forget:

irb(main):003:0> puts "We have some meaningful value" if str and !str.empty?
=> nil

But Rails makes this easy for us. Rails extends Object to add a present? method. present? is literally defined as !blank?, which is almost always what I actually want to check for: it’s not nil, or “blank”, like a string that’s just whitespace, or empty constructs like [] or {}:

irb(main):009:0> nil.blank?
=> true
irb(main):010:0> [].present?
=> false
irb(main):011:0> "     ".present?
=> false
irb(main):012:0> 0.present?
=> true

Hardly an earth-shattering development, but it’s a handy tool that seems underused.

This Exists

From Wikipedia:

“The National Helium Reserve, also known as the Federal Helium Reserve, is a strategic reserve of the United States… established in 1925 as a strategic supply of gas for airships, and in the 1950s became an important source of coolant during the Space Race and Cold War.

“By 1995, a billion cubic metres of the gas had been collected and the reserve was US$1.4 billion in debt, prompting the Congress of the United States in 1996 to phase out the reserve.”

Couldn’t they have just, you know, untied the straps and let it drift away?