How to install custom error handler for http error 429 in .htaccess?

I’m trying to add a custom error handler for error 429 for my website. I modified the .htaccess file in the root directory and added:

ErrorDocument 429 /err429.html

at the end to what it already contained, or:

RewriteEngine on

# Define priority of which index file is used first
DirectoryIndex index.php index.htm index.html

# Error pages:
ErrorDocument 401 /err401.php
ErrorDocument 403 /err403.php
ErrorDocument 404 /err404.php
ErrorDocument 500 /err500.php

But it doesn’t seem to do anythng if I trigger that error from my php script as:

http_response_code(429);

It displays web browser’s default error page.

What am I doing wrong?

The ErrorDocument directive only tells Apache what to do if a request can’t be handled/processed due to an error on the server. For example, if an URL maps to a file, but Apache can’t find it (file-existence error), then the 404 document is used. Similarly, if an URL maps to the PHP handler, but the script has a syntax error, then Apache will use the 500 document.

Conversely, the ErrorDocuments aren’t used when a request completes without error. Note that HTTP response codes are not errors to Apache. A PHP script can use any valid response code, and Apache will pass the response back to the client exactly as-is.

So, in your example, the PHP script set’s the response code to 429 and exits successfully. Because the script didn’t fail with an error (no syntax error, memory-error, etc), Apache treats it like any other PHP request and sends the response as-is to the client.

If you want to display a message with the response, then the PHP script will have to output it. For example (all code untested):

http_response_code(429);
echo "Whoa there! Slow down those requests!";

If you want to reuse the 429 message elsewhere, then you could do something like:

http_response_code(429);
include 'err429.html';

thanks. I didn’t know that you can continue sending html after an http error code response. Wouldn’t it automatically revert it to 200 then?

BTW, is there a way to check that in Google Chrome. With my http_response_code(429); the dev panel would show their built-in error page and not what my server was sending.

The Network tab of Chrome’s Developer’s Tools shows the Headers/Status-Code for requests.

Oh, I see now. Thanks. The trick was refreshing the page after you hit F12 and switch to the Network tab. Otherwise it won’t be there. (I learned two things today :slight_smile:)

1 Like

A call to http_response_code() doesn’t terminate the script in the way that exit() does. You must always set the response code before sending any output. PHP’s article on header() explains in more detail (http_response_code() is a wrapper around header()).

PHP’s behavior is more understandable when one looks at the structure of HTTP Responses, which are basically human-readable text. The response code comes on the first line, and the body only after all the headers, so PHP must output the headers before any body output. For example:

<?php
http_response_code(403); 
echo("These aren't the droids you're looking for...");

Produces the HTTP response:

HTTP/1.1 403 Forbidden
Date: Wed, 08 Jul 2020 22:56:21 GMT
Content-Length: 45
Content-Type: text/html; charset=iso-8859-1

These aren't the droids you're looking for...

Yes, it makes a total sense now. Appreciate it.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.