How to Redirect Traffic from WordPress 404 Error to Plain HTML

Recently, as our website got hacked, I faced an unpleasant hosting bill, because my account usage exceeded all the possible limits of CPU, in just… one day. Naturally, after cleaning up the files and installing various security plugins, I still had to deal with a great deal of traffic that landed on (non existing) spam pages. For example, if malicious software was located at and I had deleted that file, I still had about 10,000 daily bot requests that landed on, and since that path was no longer valid, this resulted in massive 404 error traffic. While this may sound harmless, there were still thousands of (needless) website loads, just to display a 404 error. Maybe this isn’t bad, when you had real traffic , since you still hope they will click on another link and stay on the site, in my case dealing with bot traffic, it was completely useless. After searching the web, the best solution I came across was from Jordi Plana. I knew I need to switch php to html, but there was no single plugin offering this option, so I had to do it manually.

This was so much easier than I thought, all you need to do is copy this code:

add_action(‘wp’,’determine_if_404′); function determine_if_404(&$arr){ global $wp_query; if($wp_query->is_404){ header(‘Location: ‘.get_bloginfo(‘siteurl’).’/404.html’); die; } }

And add it to functions.php

Then create a 404.html page (either fancy html splash page, or just a plan one) and uploaded it to the root directory. You can see full instructions here. This is a fantastic solution, if your website is hacked or in “post hack” recovery mode, dealing with dummy traffic and overheating your WordPress database.



You may also like...