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.