How to Force 404 Errors in WordPress

By 11-07-2013   BloggingTutorialsWordPress
Tweet Share Email Share  

Sometimes you have to force 404 errors in WordPress, either because the 404 pages are not properly configured, or sometimes WordPress itself or caching plugins may make 404 errors impossible. I tried to create 404 pages to fix Google penalties, but even after unpublishing posts, the  site HTTP response headers still displayed a 200 OK working page.

404 page

I found that there are several ways to forcing 404 errors in WordPress…

Advertisements

Check 404 file exists

It is possible that WordPress is not configured properly to show a 404 page when it is required to do so. First ensure your active WordPress theme has a 404.php file in the active themes folder. If not, you can simply save a 404.php file with following text.

<?php get_header(); ?>
    <h1>Page Not Found</h1>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Check 404 file works

Then ensure that your server can identify the location of your 404.php file. First test the exact location of the file in your browser to see it loads. Then you can add the following code on top of your WordPress folder .htaccess file to let it find the file.

ErrorDocument 404 /index.php?error=404

If your WordPress installation is in a subdirectory called ‘abcd’, you might use this code.

ErrorDocument 404 /abcd/index.php?error=404

Disable Quickcache 404 Caching

We had this another issue with 404 files that at first instance it showed a 404 error, then QuickCache cached the file to create a html file for that error page. Then if you check the site headers the page gave a 200 OK status meaning the page existed well  (this was similar to the feed caching issue). So we added this code to our 404.php template page in the first line even before the header coding was called for.  Add the following code

<?php
if (is_404()) {
    define('QUICK_CACHE_ALLOWED', false);
}
?> 

Disable WordPress Smart Redirect

WordPress is a smart blogging tool and redirects 404 errors to best possible matching urls which it seems fit based on the visited url keywords. Many of my 404 urls were being redirected to similar working urls, which might or might not be relevant but had similar keywords. So we need to add the following code to theme functions.php to prevent this WordPress redirection ability to get pure 404 errors.

remove_filter('template_redirect', 'redirect_canonical');

Test Site Response Headers

So mostly this should take care of your 404 errors and you can test them with popular site HTTP headers checking tools like Web Sniffer or HTTP Response Headers to find out if your 404 errors are actually working.

404 not found error

If you get this ‘404 Not Found’ error after repeatedly refreshing the result you are done.

17 comments on “How to Force 404 Errors in WordPress

  1. Blog Master says:

    Yea you are right sometimes will get penalized so forcing 404 in wordpress is a great idea.Thanks for sharing this idea with us!

  2. piyush001 says:

    yup man this is really working and helped to remove 404 errors…

  3. bottom12 says:

    wow excellent tips about 404 errors, i am new designer it’s really help for me thanks

  4. Local SEO says:

    404 errors are killing one of my clients websites because they removed a bunch of pages before consulting me. now I have to manually go through and create redirects.

  5. Mangalore says:

    Hi… First test the exact location of the file in your browser to see it loads. Then you can add the following code on top of your WordPress folder .htaccess file to let it find the file.thank you for the share..

  6. Elex says:

    Thanks a lot for this article, it was very helpful !!!

  7. This post is very worthy for me. A great my problem is solved by this post. Thanks

  8. Yes I can hit pretty much all other files without problem. I’m assuming if there’s another PHP file with similar naming convensions the same would happen (ie somefile.js.php or somecss.css.php ) I’m turned off the pretty URL rewriting and it seems to have fixed the 404 but I don’t get the nice permalink structure.

  9. Badootech says:

    Nice and excellent share…its definately worth trying

  10. Rickymartin says:

    I used Permalink Validator to solve some of the issues that I had with wrong URLs typed but the most annoying problem still remains.

    When someone clicks on a link to my blog that ends with /?something like ?download_free_movies it still directs them to a page on my blog and I would like them to see a 404 not found page instead.

    My wordpress website was recently hacked and I have hundreds of those links listed on google and other search engines and websites. I am in the process of trying to remove them from google cache but returning a 404 instead of directing it to a page on my website would be very helpful as well.

  11. Hello,

    I have WordPress website and would not find 404 errors but your article was very helpful not only it guided me but also help me to use right code.

    Thanks

  12. munna says:

    Nice tips . This will really help me to customize and addition of 404 error page

  13. Ravi Chahar says:

    It’s normal to get 404 error. When we get these errors at a greater extent then it will create problem because we can get penalized by Google. I was also seeking for the solution. How to fix it? You have provided such a useful information about fixing 404 error. Thanks for the post.

  14. I am not a technical guy and due to this I was facing some problem to fix 404 not found error in my blog. I followed your tips and now all errors have been fixed. Thanks to share such useful tips with us.

  15. carte itunes says:

    thanks for this wonderful blog. it is very helpful to others who spend there priceless time in searching solution for this.

  16. Rakesh Desai says:

    Wow. Thanks for this information. Putting 404 errors in wordpress helps to solve this problem of penalizing. Thank you.

  17. Trivedi Jay says:

    Nice Article , Thanks for sharing useful tips with us. I am new blogger so i am very thankful for your article

Leave a Reply

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

css.php