Custom WordPress 404s (plural) in .htaccess

I need to catch all the 404’s in my wordpress site and redirect them according to the original url request. For instance, if the following is a 404

http://example.com/news/123.html

I need to redirect it to

Read More
http://example.com/news

And I will have multiple instances of this, like the following 404

http://example.com/hats/greenhat.html

Will go to

http://example.com/hats

I can’t figure out how to do this in WordPress. In my .htaccess file, this is my first line

ErrorDocument 404 http://example.com/my-custom-404.php

But instead of redirecting to my-custom-404.php, it still goes to the default wordpress 404 page.

Here is my htaccess for reference

 ErrorDocument 404 http://example.com/index.php


 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase / 

 #Rewrite 404s to the news section
 RewriteCond %{REQUEST_URI} ^/404/$
 RewriteRule ^(.*)$ /news/index.php [R=301,L]

 RewriteRule ^index.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d

 RewriteCond %{HTTP_HOST} !^example.com$ [NC]
 RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

 # THE 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>

In my file as a test I am trying to rewrite all 404 to /news/index.php, which isn’t working – everything is still going to the wordpress 404 page

Related posts

Leave a Reply

1 comment

  1. WordPress has it’s own way of handling 404. Inside your WP theme you can create a file called 404.php and inside that file have a PHP code to:

    1. Read the current request URI using $_SERVER['REQUEST_URI']
    2. From the URI find last index of /
    3. Using substring function get rid of part after last / and redirect