How to control error pages in WordPress

By default WordPress searches for error pages, such as error 404 (file not found) or error 403 (permission denied), in the current active theme (e.g. 404.php or 403.php). If the theme does not provide default error pages WordPress’ built-in ones will be used instead.

Since I tend to deploy other web applications alongside WordPress, my overall goal was to have the same error pages independent of the application context in order to achieve a standardized representation. Thus, for instance if a page cannot be found the current WordPress layout will be used to present the error.

Using Apache default error pages can be controlled via .htaccess files. So first, create these custom links to your error pages:

ErrorDocument 403 http://your.server/error-403-permission-denied
ErrorDocument 404 http://your.server/error-404-file-not-found
ErrorDocument 500 http://your.server/error-500-internal-server-error

The next step is to create these WordPress error pages:

error-403-permission-denied
error-404-file-not-found
error-500-internal-server-error

Finally, in your currently active theme create the corresponding error pages:

403.php
404.php
500.php

Finally, all you need to do is to redirect to the link specified in .htaccess inside your theme’s error pages, e.g. for 404.php:

header('Location: http://your.server/error-404-file-not-found');
exit;

That’s it. Now you have a standard layout for error pages independent of the application context on your server.

You can try it on this site by requesting a non-existing file, such as https://www.kerstner.at/does_not_exist_on_this_page.

Further improvements could be made for sites with multiple languages, for instance by prepending the language when redirecting via the corresponding error pages.

You may also like...

3 Responses

  1. Kaspar says:

    Wie heisst das Plugin an der Seite ? Das brauche ich!

    • Hallo Kaspar,
      es handelt sich hier um kein WordPress Plugin sondern vielmehr um eine eigene Lösung, wie man serverweite Fehlermeldungen mittels WordPress erzeugen kann. Die Anleitung verwendet die eingebaute Funktion von WordPress-Themes spezielle Fehlerseiten zu liefern. Ich mache mir das hier zu nutze, indem ich Fehler auf die entsprechenden WordPress “pages” weiterleite um ein einheitliches Layout zu erhalten.

  2. User says:

    From all the methods posted out there this is the only one that worked perfectly right from the beginning without changing functions.php or what not.

    Might I add that if you create a page page-error-403-permission-denied.php you can style the error page individually outside of the WP panel editor for that page.

    Complete control over error pages, wonderful, thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *