Apache: create a custom error page

Apache: create a custom error page

Error 404When you are using Apache as a web server and you want to create a custom error page, you can do it by defining an ErrorDocument in your config file (httpd.conf) as follows:


ErrorDocument 404 /errors/404.html


This will show the page ‘404.html’ when the user accesses an unknown page. The path needs to be relative, because using a absolute path will probably give a 302 error. Because a relative path will always refer to the current website and thus this will only work for this site.

But if you are hosting more sites (using virtual hosts) and want to use the same error page for all the sites, you can create an alias for ‘/errors/’, so for all site this directory will exist.

You create the alias as follows:

[code]Alias /errors/ d:/wwwroot/errors/[/code]

In this example the ‘/errors/’ directory will point the the absolute path ‘d:/wwwroot/errors/’ on your web server.

This is not all, because we didn’t set any permission for the absolute directory. Without doing this, the error page cannot be accessed. Use the following configuration to set permissions for the directory.


<Directory d:/wwwroot/errors/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from All
Satisfy Any

Now your web server will show your custom error page. In this example a 404 error page was configured, but for other error codes the configuration is the same.

Tip: Sending a 404 error by code
If your site has directories where files needed by your site are stored, but should not be viewable by anybody (if browsing is allowed by the web server), you can always put an index document in the directory which will show an empty page or something like that.

An alternative would be to redirect the user to the 404 error page we just configured. If your web server is running PHP, then putting the following code in ‘index.php’, will do the trick:

header( "HTTP/1.1 404 Not Found" );
header("Location: /errors/404.html");