tags:

views:

439

answers:

3

Hi folks,

I'm about to do some server maintenance and would like to rewrite requests to Apache's standard 503 temporarily unavailable message.

I've Googled and found a few mod_rewrite snippets, but they all involve doing an [R=503] to a PHP script which then sends its own 503 headers and a hand-written message... which just seems ugly to me, when Apache's default is there for me to use.

So, how can I rewrite all request to Apache's standard 503 error message?

Thanks for your help

(Long time Coding Horror RSS subscriber here... Just in a bit of a rush!)

A: 

I don’t know exactly since when mod_rewrite allows other status codes than 3xx for the R flag. But this should do it:

RewriteEngine on
RewriteRule ^ - [L,R=503]

And if you’ve set a custom error document, use this to prevent additional internal errors:

RewriteEngine on
RewriteRule !^path/to/my-custom-error-document$ - [L,R=503]


Edit   Found a better solution using the REDIRECT_STATUS environment variable:

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteRule ^ - [L,R=503]
Gumbo
+2  A: 

To add a little bit more, I often have the following:

RewriteEngine on
RewriteCond -f path/to/my-custom-error-document
RewriteRule !^path/to/my-custom-error-document$ - [L,R=503]

What this does is check to see if the error document exists. If it does, it serves up the error page. What this allows me to do is throw up the custom error without having to restart apache. To put it back in place, I just remove (or rename) the error page.

Rob Di Marco
A: 

Thanks guys - worked perfectly.

Here's the exact .htaccess configuration I used (sans my IP address) during maintenance:

RewriteEngine on
RewriteCond %{REMOTE_HOST} !^123\.456\.789\.012$
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteRule ^ - [L,R=503]
Abignale