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
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • 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...

Incredible Demos

  • By
    JavaScript Speech Recognition

    Speech recognition software is becoming more and more important; it started (for me) with Siri on iOS, then Amazon's Echo, then my new Apple TV, and so on.  Speech recognition is so useful for not just us tech superstars but for people who either want to work "hands...

  • By
    TextboxList for MooTools and jQuery by Guillermo Rauch

    I'll be honest with you: I still haven't figured out if I like my MooTools teammate Guillermo Rauch. He's got a lot stacked up against him. He's from Argentina so I get IM'ed about 10 times a day about how great Lionel...

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!