43

RFC2616, 503 Service Unavailable

The server is currently unable to handle the request due to a temporary overloading or maintenance of the server

How to configure Apache 2.2 to serve particular name based virtualhost 503 code with custom HTML page?

3 Answers 3

54

503 Temporarily Unavailable, with trigger

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteCond "/srv/www/example.com/maintenance.trigger" -f
RewriteRule ^(.*)$ /$1 [R=503,L]

If the maintenance.trigger file exists, Apache will serve up a 503 Service Unavailable response. To serve up a custom "down for maintenance" page, use ErrorDocument to specify the file, like so:

ErrorDocument 503 /503_with_cats.html

Enjoy!

3
  • Does the constant checking of a file not slow down a request?
    – shennan
    Commented Apr 21, 2015 at 1:19
  • 1
    Good question! I haven't done any benchmarks. Am assuming the OS filesystem cache keeps things speedy. Commented Apr 28, 2015 at 5:04
  • 7
    It is misleading to specify the target part in rewrite rule, I prefer: RewriteRule ^ - [R=503,L]
    – brablc
    Commented Nov 11, 2015 at 18:52
32

You could use mod_rewrite:

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteRule !^/down/for/maintenance$ %{DOCUMENT_ROOT}down/for/maintenance [L,R=503]

The RewriteCond directive makes sure that no additional internal error occurs when redirecting to a custom error document.

5
  • 1
    Great, but how to specify custom "maintenance" page?
    – temoto
    Commented Mar 7, 2009 at 20:55
  • 7
    ErrorDocument 503 /down/for/maintenance/index.html
    – flurdy
    Commented Jan 17, 2012 at 16:12
  • My error document refers to static content on file system so am getting 503 for static content its refereeing like 11.11.11.11 - - [15/Mar/2016:13:24:31 -0500] "GET /core/css/styles.css HTTP/1.1" 503 1985 how do I make sure my error document can load static content without getting 503
    – OTUser
    Commented Mar 15, 2016 at 18:27
  • 1
    @RanPaul You can extend the RewriteRule pattern or add a RewriteCond directive to match these URLs as well. For example: RewriteRule !(^/down/for/maintenance$|^/core/css/) …
    – Gumbo
    Commented Mar 15, 2016 at 19:31
  • It always redirects to maintenance page Commented Nov 3, 2017 at 9:39
0

@temoto

To specify a 503 for bots and a maintenance page for humans, try the following

RewriteEngine on
RewriteBase /
#for bots such as google
RewriteCond %{HTTP_USER_AGENT} ^.*(Googlebot|Googlebot|Mediapartners|Adsbot|Feedfetcher|bingbot)-?(Google|Image)? [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteRule !^/down/for/maintenance$ %{DOCUMENT_ROOT}down/for/maintenance [L,R=503]

#for humans
RewriteCond %{REMOTE_HOST} !^1\.1\.1\.1
RewriteCond %{REQUEST_URI} !^/maint.html [NC]
RewriteRule .* /maint.html [R=302,L]
6
  • 16
    why use a separate page for humans? 503 pages can contain content, and browsers will display it.
    – Carl Meyer
    Commented Jan 13, 2010 at 21:01
  • In some cases, tomcat may not be able to serve a 503 page. Thus, we let Apache serve a static page. Commented Jan 14, 2010 at 4:07
  • 1
    But this page doesn't have to be different for humans and bots... that difference doesn't make any sense to me.
    – Calimo
    Commented Nov 5, 2013 at 12:45
  • @AlexanderN why would you be less friendly to bots?
    – Calimo
    Commented Nov 6, 2013 at 10:36
  • 4
    @AlexanderN you can send the instructions to the bot as well, what's the problem with that? Anyway you're doing it already (except Google and a handful others) as your regex doesn't catch all bots.
    – Calimo
    Commented Nov 6, 2013 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.