In this guide we are going to talk about better handling 404 page not found errors in WordPress. This can be a problem when the WordPress theme template is used for all 404 errors, running unnecessary PHP code and MySQL queries.
This guide is written for WordPress websites on our Linux hosting enviroment.
What is a 404 page
A HTTP 404 Not Found error means that the content you were trying to load could not be found on the server. This is a client side error meaning that the page has been deleted or moved and the address was not updated or redirected.
Why is WordPress creating load when generating 404 pages
When a visitor requests a file or page that doesn’t exist, WordPress displays a 404 not found page that can include, widgets, archived pages, links, a search form etc. Now these custom 404 error pages might look good and have some useful things on them, but when loaded it creates unnecessary PHP & MySQL queries using server resources it doesn’t really need to.
Checking for 404 pages
You can use the following tools to check for 404 pages on your website.
Online tools
Browser tools
- FireFox – Tools, Web Developer, Network (Ctrl + Shift + q)
- Chrome – Menu, More tools, Developer tools (Ctrl + Shift + i)
Error logs
Don’t run WordPress for missing static files
You may still want to display a custom 404 page for pages, but generating a custom 404 page for static files (Images, CSS, JavaScripts, etc) is not recommended.
To stop WordPress loading a custom 404 page for these file types we need to add the following to a .htaccess file and setup a 404 page for static content.
Add the following lines to the .htaccess file before, # BEGIN WordPress:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(robots\.txt|sitemap\.xml(\.gz)?) RewriteCond %{REQUEST_FILENAME} \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ [NC] RewriteRule .* - [L]
Adding static 404 pages for static content
We now need to create a simple html page for the 404 error. This can be added in the hosting control panel, or via the .htaccess file.
In the Control Panel
A comprehensive guide for setting up error pages in the hosting control panel.
In a .htaccess file
Create a .htaccess file, add the following to the top of the file
ErrorDocument 404 /404.html
Then create a simple 404.html page with 404 error not found message and upload to the root of your WordPress install.
Miscellaneous
Beware that some RSS readers may try to load a Favicon file/image (favicon.ico) the little square 16px X 16px image you will see if you bookmark a page. If this doesn’t exist, it will also create a 404 page. Since WordPress 4.3 these favicons can be added from the WordPress dashboard. So no excuse not to have one!
Apple devices do a similar thing as RSS readers, searching for files that might not actually exist. You could see lots of 404 errors in your logs for the following files, apple-touch-icon.png and apple-touch-icon-precomposed.png. Again the easiest way to handle these 404 errors is to create these files and place them in your WordPress root directory. You can find more information about these files on the iOS developers website
I’ve tried to cover most things in this guide, hopefully the above will help you fix any 404 errors on your WordPress site.
Classification: Public
Last saved: 2024/05/22 at 12:39 by Jamie