Articles

How to Change the Apache Error Messages

You can change the Apache error messages for a single app by adding ErrorDocument directives to the app's .htaccess file. For example, any of these formats work:

ErrorDocument 403 "Sorry, access to this page is forbidden."
ErrorDocument 500 http://example.com/server_error.html
ErrorDocument 503 /errors/service_unavailable.html

The format that uses an http or https URL will redirect the browser to the specified page.

To change the error pages globally, log in as root and create the following file:

/etc/apache-sp/conf.d/errors.conf

In that file, include this content:

ErrorDocument 403 "Sorry, access to this page is forbidden."
ErrorDocument 500 http://example.com/server_error.html
ErrorDocument 503 "Service Unavailable"

Then, restart Apache by running the following command as root:

sudo service apache-sp restart

Note that you should not use the path to a file when configuring this globally.

Changing 404 Error Messages

You can change the messages Apache sends for 404 errors by entering the following line in your app's .htaccess file:

ErrorDocument 404 /404.php

This format assumes you have a file called 404.php in your app's public directory.

However, this ErrorDocument directive will not catch 404 errors for requests that end in .php themselves. For example, a request for /this-does-not-exist.php will not be handled by the ErrorDocument directive because the request ends in .php.

To handle 404 errors for requests that end in .php, you should use rewrite rules that direct those requests to a specific PHP script, which will then send a 404 response.

To show a custom 404 page (using a file named 404.php, for example) when a requested script doesn't exist, add the following to your app's .htaccess file:

RewriteRule ^404\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]

Then, in that 404.php script, you would have something like this:

<?php
header("HTTP/1.0 404 Not Found");
// generate the HTML of the response here
?>