33

I need to return a 503 status code from one of my sites while it's down for maintenance, in the time-honoured SE_firendly fashion. I can't seem to work out how to do this without invoking external scripts, which I'd rather avoid.

Is there an apache directive which will allow me to return an arbitrary HTTP status code without resorting to hacks like invoking a php script which sets the status header?

dan mackinlay
  • 433
  • 1
  • 4
  • 5
  • 2
    I'm not quite sure that running a PHP file classifies as a "hack"... that's sort of exactly what a web server is designed to do... – Mark Henderson Aug 12 '09 at 03:55
  • 3
    Hm. I think invoking a whole interpreted scripting language to set a single line of the server response does qualify as hack. Setting lines of response headers without elaborate workarounds seems to me to be closer to what web servers were designed to do. – dan mackinlay Aug 13 '09 at 16:35
  • Same question at http://webmasters.stackexchange.com/questions/61241/how-do-i-temporarily-disable-a-website?noredirect=1#comment64369_61241 – rubo77 May 01 '14 at 08:12

3 Answers3

37

This serves every request a static holding page along with the 503 status.

RedirectMatch 503 ^/(?!holding\.html)
ErrorDocument 503 /holding.html
Header always set Retry-After "18000"

RedirectMatch is used to negate the holding page itself which would otherwise create an infinite loop.

mod_header is used to set a Retry-After header so that you can tell Google/other bots etc that you should back up after 18000 seconds (5 hours) in this example. You can sudo ap2enmod header to activate mod_header (which is required for the Header directive).

Werner Henze
  • 133
  • 5
muffinresearch
  • 831
  • 6
  • 8
  • 2
    +1: A side comment: You can only add [RedirectMatch 503 .*] and the [Header always set Retry-After "18000"] rules and it works with the default 503 error message. Check for conf.d/localized-error-pages file to set a different message for different languages (those require negotiation, alias and include modules). – lepe Apr 20 '12 at 01:08
  • It works! For my personal website, I use `RedirectMatch 503 ^/(?!\.Error/.*|dragonlord-bg\.jpg)`. – bwDraco Feb 09 '14 at 04:09
8

Try

Redirect 503 /

From https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect :

Other status codes can be returned by giving the numeric status code as the value of status. If the status is between 300 and 399, the URL argument must be present. If the status is not between 300 and 399, the URL argument must be omitted.

Additional Note: This will display the default/configured ErrorDocument for the 503 Error.

tstenner
  • 103
  • 4
David Pashley
  • 23,151
  • 2
  • 41
  • 71
  • Thanks david. mod_alias isn't where I expected to find that answer, but it seems to do the job. – dan mackinlay Aug 13 '09 at 16:37
  • yeah, Redirect isn't really the right verb for what you're doing :) – David Pashley Aug 13 '09 at 16:58
  • 1
    I get an error doing that: "Redirect takes two or three arguments, an optional status, then document to be redirected and destination URL", can you give a more complete example in context? – Tom Sep 24 '11 at 03:20
  • Redirect 503 it's missing one argoment, URL-path, which is always mandatory. URL on the other hand must be omitted if the redirected code is not 300-399 –  May 18 '10 at 15:32
  • 6
    `Redirect 503 /` seems to work. – mpartel Aug 24 '14 at 23:58
6

To make your maintenance mode more flexible, set 503 when a special file exists, ala:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/.maintenance -f
RewriteRule .* - [R=503]

NOTE: The location of DOCUMENT_ROOT can differ between httpd 2.2 and 2.4

This way all you need to do is "touch /path/to/docroot/.maintenance" and your site will instantly start returning 503's. Then to make it all purdy, create a custom nicely formatted html error page and add this to the correct apache server's config file:

ProxyErrorOverride on
ErrorDocument 503 /error/503.html

And for bonus points if you have multiple web servers either make the location of .maintenance on a shared filesystem, or for even more bonus points:

pdsh -w $web1_ip,$web2_ip,$web3_ip "sudo touch /path/to/docroot/.maintenance"

The advantage of making the maintenance mode file based is that it works when your application doesn't, it's quick and simple, and it's easier for a frontend caching proxy to handle because you can handle all backend 503's in one fell swoop.

Sources:

Phil
  • 61
  • 1
  • 5