Optimizing WordPress 404’s

One of the great things about WordPress is how 404 error pages are handled. If a page isn’t found then you can show a proper dynamic error page giving the user things to do – this removes a lot of the traditional problems with 404 pages, ie the dead end syndrome.

404 is the server error code that is generated for content that doesn’t exist.

However these new 404 pages are a lot slower than normal 404’s, they require database contact, and PHP processing so if you have a lot of them they can slow down your server. Most times the benefits outweigh the negatives however there is one time when this isn’t the case.

Images and other media that are not loaded, do not need a full 404 page. If you’re linking to an image that doesn’t exist then you could create a lot of extra work for your web server.

How to make it work

To stop the 404 page from executing fully I look at the url and work out if it’s a url for a blocked file type (jpg, css, js, etc). If the file is in the list of bad file types then it gets stopped and a message is displayed.

Where to use it

This function should be added to your themes functions.php file. To execute it I placed it at the top of my 404.php template page, however in hindsight I could probably also use an action instead and may just tweak my theme to use that instead. There’s nothing like writing about something to make the problems clear! 🙂

Do you have any ideas for other things I could do to improve this code? Do you think it’s a good idea?

This only works for static file types, because of the way your server serves these things: for images etc. it first tries to find the file on your server and serves it immediately without loading WordPress. If it can’t find the file, it defaults to loading WordPress, then when you have that function you wrote and do the add_action as above, it’ll immediately output and stop right there.

By the way, W3 Total Cache has an option where you can set it to quit even sooner, it adds some lines to your .htaccess file that essentially do the same without even booting WordPress at all.

I did try adding the code to the init hook, but WordPress hasn’t decided if the content is a 404 or not by that time so I have moved it to the template_redirect hook which seems to work quite well, and cleans up my templates a little.

The funny thing is, when it’s a static file, if you’re not running any weird plugins, WordPress doesn’t have to decide whether it’s a 404. If WordPress is being called for a static file, it is a 404 otherwise the web server would have served the file directly.

Hmm – that’s interesting. I thought WordPress took care of paged comments with it’s rel=canonical, and nofollow settings. I wonder if I can build support for that into my theme…

The reason for the paged comments is that I have a number of posts with massive amounts of comments (500+), and even more with 100+ comments. I’ve bumped up the number of posts per comment page but that won’t help with this issue since the comments still link to the comments pages.

WordPress does set the canonical correctly, but it’s still not ideal, ideally the comments after 100 or so would be served using JavaScript… Funny thing is, the issue is not with the comments, even at 500 that’ll hardly affect pageload, it’s with the gravatars…

Given I brought this article to Joost’s (Yoast’s) attention, I figured I’d chime in and say it’s been interesting to read. I implemented the stuff in this article into my 404 page via functions, but I haven’t tried via init as of yet. And W3TC does have the 404 option for static files via the “Browser Cache” page (not sure why it’s there). And I guess you could customize the 404 code with that as well by setting the “Error 404” entry in the .htaccess.