37

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?

3
  • 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... Commented Aug 12, 2009 at 3: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. Commented Aug 13, 2009 at 16:35
  • Same question at webmasters.stackexchange.com/questions/61241/…
    – rubo77
    Commented May 1, 2014 at 8:12

3 Answers 3

39

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

2
  • 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
    Commented Apr 20, 2012 at 1:08
  • It works! For my personal website, I use RedirectMatch 503 ^/(?!\.Error/.*|dragonlord-bg\.jpg).
    – bwDraco
    Commented Feb 9, 2014 at 4:09
11

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.

5
  • Thanks david. mod_alias isn't where I expected to find that answer, but it seems to do the job. Commented Aug 13, 2009 at 16:37
  • yeah, Redirect isn't really the right verb for what you're doing :) Commented Aug 13, 2009 at 16:58
  • 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
    – user33469
    Commented May 18, 2010 at 15:32
  • 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
    Commented Sep 24, 2011 at 3:20
  • 6
    Redirect 503 / seems to work.
    – mpartel
    Commented Aug 24, 2014 at 23:58
7

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:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .