Question

I want to create a custom 404 page, but I cannot seem to override the 404 which is in Wordpress itself. The reason I want a custom 404 page is that if I get 404 within my subdirectories I get directed to my wordpress-site's 404. I'd rather have a 404 for my whole domain.

I've only found solutions to do this within Wordpress-pages etc, but do I really have to? I guess my problem is the .htaccess-file, however if I mess this one up, my links will be broken on my Wordpress-site.

My .htaccess looks like this (although the first line is the one I want to add, but wordpress doesn't seem to care).

ErrorDocument 404 /404page.php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

In short: My Wordpress-site is on the root of my domain and its 404 page is shown on all subdomains, I want to override this with my own.

Was it helpful?

Solution

When a URL is processed by Wordpress, its index.php script is loaded with the path to the requested page. Because in this case the index.php script is always found, a normal 404 error will never trigger (via ErrorDocument 404 …). If the requested URL does not match a page that WP knows about, WP will display its own "404" page.

Unfortunately, if WP processes a URL, there is no way to use a normal 404 page, bypassing the WP 404 page. This issue is documented at https://core.trac.wordpress.org/ticket/7592.

OTHER TIPS

Edit your 404.php in your theme folder or add a 404.php in your child theme folder, so it contains your error message. It will not bypass WordPress, but it is the only way. However, if you want to gain some speed, you can go for a quick redirection to a static page by putting this content into the theme's 404.php:

<?php if (!defined( 'ABSPATH' )) exit;

header('HTTP/1.0 404 Not Found');
readfile( ABSPATH . '/wp-content/static/404.html' );
exit;
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top