Page 1 of 1

Deliberately trigger a response error code

Posted: Fri Aug 31, 2007 7:53 pm
by Ollie Saunders
How can I go about deliberately instructing Apache 2.0.33 to generate an error "503 Service Unavailable" and then display the ErrorDocument assigned to that error. Modifying configuration files or using PHP is acceptable.

Posted: Sat Sep 01, 2007 4:22 am
by stereofrog
for example

RewriteEngine On
RewriteRule .* - [R=503]

Posted: Sat Sep 01, 2007 4:41 am
by s.dot
maybe..

Code: Select all

header('HTTP/1.0 503 Service Unavailable');

Posted: Sat Sep 01, 2007 4:58 am
by Ollie Saunders
stereofrog wrote:RewriteEngine On
RewriteRule .* - [R=503]
Cool I'll give that a try.
scottayy wrote:

Code: Select all

header('HTTP/1.0 503 Service Unavailable');
But it won't then display the ErrorDocument will it.

Posted: Sun Sep 02, 2007 12:27 am
by s.dot
ole wrote:
stereofrog wrote:RewriteEngine On
RewriteRule .* - [R=503]
Cool I'll give that a try.
scottayy wrote:

Code: Select all

header('HTTP/1.0 503 Service Unavailable');
But it won't then display the ErrorDocument will it.
I imagine it would, if you had .htaccess set up that way.

Code: Select all

ErrorDocument 404 /path/to/503.html
Seems like more of a hack, though. I think stereofrog's solution (providing it works) would be a better solution.

Posted: Sun Sep 02, 2007 7:24 am
by Ollie Saunders
I imagine it would, if you had .htaccess set up that way.
Correct me if I'm wrong. But once Apache has begun to serve or, in the case of PHP, execute, a file nothing you do inside that file is going to stop it and allow it to start serving another. Once you're into a PHP script you can't then tell Apache to go off and serve a different file instead, although there are things you can do inside PHP to make it seem that way, virutal() for instance. Even by using virtual() I still need to write something that generates a 503 somehow unless I can parse my apache2.conf for the correct ErrorDocument declaration and just include that in. On my server accessing apache2.conf is strictly out of bounds as is the use of virtual() as they both are potentially insecure.
Seems like more of a hack, though. I think stereofrog's solution (providing it works) would be a better solution.
Yep I'm going to go give it a whirl now.

Posted: Sun Sep 02, 2007 8:07 am
by Ollie Saunders
Ahh balls

Code: Select all

RewriteRule: invalid HTTP response code for flag 'R'

Posted: Sun Sep 02, 2007 8:18 am
by volka
Are you using a 1.3.x httpd ?

Posted: Sun Sep 02, 2007 8:36 am
by Ollie Saunders
2.0.33

Perhaps I'm using the wrong status code here. I want to be able to take a site down and have the assigned ErrorDocument be displayed. This drew me to 503:
Shifflet wrote:503 Service Unavailable

For any case in which the Web server is unable to satisfy the request temporarily, it will use a 503 Service Unavailable status code in its response. It can also include a Retry-After header to indicate when the Web client will be able to try the request again and likely receive a successful response.
But it would seem Apache deems 503 to be something that I shouldn't be capable of generating myself. None of the other error codes seem appropriate.

Edit:
I just tried Redirect

Code: Select all

Redirect 503 /foo http://www.google.com
and apache said

Code: Select all

Redirect URL not valid for this status

Posted: Sun Sep 02, 2007 8:49 am
by Ollie Saunders
Judging from this there's no way just to bounce to the assigned error documents other than rewriting to them directly, so that's what I'm going to do. Once there you can use header() as Scottayy suggested.

Posted: Sun Sep 02, 2007 9:09 am
by stereofrog
Both R=503 and Redirect 503 work for me on apache 2.2.4. So I guess it's a new feature of 2.2 series. Sorry for misinfo. ;(

Posted: Sun Sep 02, 2007 9:11 am
by volka
Oops, sorry. The behaviour of R=xyz has been changed in 2.2 not 2.0
http://svn.apache.org/viewvc/httpd/http ... rev=571986