WordPress, 404s, and Load Time

By  on  

It's no secret that my website tends to get slow during high periods of traffic and I believe I identified one of the big problems. I've found that one of the plugins I use has an incorrect ".css" reference that is 404ing. That means, of course, that instead of loading a CSS file, it's loading my 404 page. Long story short, the page is essentially loading at least twice. No more of that. Although I have put a blank file where that CSS reference is supposed to go, I've thought of a way to not let that happen again.

The PHP

header('HTTP/1.1 404 Not Found');
$uri = strtolower($_SERVER['REQUEST_URI']);
if(strstr($uri,'.css') || strstr($uri,'.js') || strstr($uri,'.txt') || strstr($uri,'.jpg') || strstr($uri,'.gif')) { die(''); } 

Inside the 404.php file, I sniff for a file extension of JS, CSS, etc. If I find a match, I simply "die out" the page which prevents the 404 page from loading. However if the user legitimately hits a 404 page, I display the "sorry..." page.

Of course this isn't the only issue (I think Dreamhost may have slow servers) but this should help out a lot!

Recent Features

  • By
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

  • By
    Interview with a Pornhub Web Developer

    Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...

Incredible Demos

  • By
    Introducing MooTools ScrollSide

    This post is a proof of concept post -- the functionality is yet to be perfected. Picture this: you've found yourself on a website that uses horizontal scrolling instead of vertical scrolling. It's an artistic site so you accept that the site scrolls left to right.

  • By
    Fx.Rotate:  Animated Element Rotation with MooTools

    I was recently perusing the MooTools Forge and I saw a neat little plugin that allows for static element rotation: Fx.Rotate. Fx.Rotate is an extension of MooTools' native Fx class and rotates the element via CSS within each A-grade browser it...

Discussion

  1. Since originally writing this article, I’ve made this check as just for the “.” character:

    if(strstr($uri,'.')) { die(''); } 

    Performance++!

  2. $ext = !empty($_SERVER['REQUEST_URI']) ? strtolower(pathname($_SERVER['REQUEST_URI'], PATHNAME_EXTENSION)) : null;
    if(in_array($ext, array('jpg', 'css', 'whatever')) die('');
    

    I like this one better :>

  3. Rich

    This is pretty interesting David. I did place a little code on my own 404’s that tells me more information as to the users interaction. What page they were trying to get to and some other stuff that for the most part help me either correct the issue or at least konw whats going on and try to correct the problem.

  4. Hi David

    Great blog, a few suggestions for this entry though:

    Have you considered just fixing the plugin so the visitor to your site do not have to “waste” a round trip to the server just to get an empty .css file? It seems to me to be the better fix for the problem you describe.

    Your general “catch future errors” fix is OK. I would add some logging to it though, so you get notified when your page and/or a plugin is looking for a resource that is not there, and you can fix it rather than have it silently continue wasting visitors time with round trips to the server.

    Regards, Egil.

  5. @Egil: I did fix the plugin. Another reason I do this is because spammers try to hit my site all the time. For example:

    http://davidwalsh.name/?russian-porn-site/chicks.txt

    I don’t want then straining my resources.

    If it’s an article bombing out, it wont have a “.” in it so I’m not worried about legit 404s not being caught.

  6. Hey David,

    Brilliant solution to one of the nagging problems with wordpress plugins. So many of them load missing files that don’t seem to be necessary, but slow down the page load.

    I tried it out on my site and definitely seemed to notice some improvement.

    Thanks fo sharing!

    -Marty

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!