AeJey
AeJey

Reputation: 1457

Redirecting to custom 404 page in home directory

I am having trouble redirecting the 404 (incorrect url requests) to a 404 page (404.php) I developed and uploaded to the server.

The server has a Wordpress installation in the sub-folder /blog and all the 404 are getting redirected to wordpress blog's 404 page. I have set the redirection in .htaccess but is not working.

ErrorDocument 404 /404.php

This is what I added in .htaccess, but it still redirects to the blog's 404 instead of the 404 page in root.

How can I override this?

Upvotes: 1

Views: 1733

Answers (3)

AeJey
AeJey

Reputation: 1457

I achieved it by pasting this code on top of my wordpress theme's header.php file

 if ( is_404() ) {
      wp_redirect( '../404.php' );
      exit;
   }

And now the root/main site 404 page is getting loaded instead of wordpress blog's 404 page as intended. :)

Upvotes: 1

Touqeer Shafi
Touqeer Shafi

Reputation: 5264

In my understanding ErrorDocument 404 /404.php will only work if you have not specified rewrite rule in .htaccess eg:

# 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

Because wordpress has it's own life cycle which uses URL rewrite in .htaccess to determine which page request are coming. WordPress has it's core mechanism to understand the current request. For example you have created a page name hello-word, when you go to http://your_wordpress.com/hello-word wordpress determines hello-world from URL and find in it's database whether it has hello-word page or not. If current page does not exist in database then it's mechanism automatically call theme 404 template which is located in currently active theme.

Solution

Create a file in wp-content/themes/YOUR-THEME/404.php and change it's content. You don't need to specify ErrorDocument 404 /404.php in .htaccess. Read more how to create 404 template here

Upvotes: 1

maddy
maddy

Reputation: 121

first of all make sure that the 404.php is present in the proper location and http://www.example.com/404.php is working properly.

if both 404.php and .htaccess are present in the same root folder, then try changing following line in .htaccess file

ErrorDocument 404 /404.php

to

ErrorDocument 404 /root/404.php

Upvotes: 0

Related Questions