Deliberately trigger a response error code

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Deliberately trigger a response error code

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

for example

RewriteEngine On
RewriteRule .* - [R=503]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

maybe..

Code: Select all

header('HTTP/1.0 503 Service Unavailable');
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Ahh balls

Code: Select all

RewriteRule: invalid HTTP response code for flag 'R'
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Are you using a 1.3.x httpd ?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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. ;(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply