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
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

  • By
    background-size Matters

    It's something that makes all men live in fear, and are often uncertain of. It's never spoken, but the curiosity is always there. Nine out of ten women agree in the affirmative. Advertisers do their best to make us feel inadequate but...

  • By
    Multiple Backgrounds with CSS

    Anyone that's been in the web development industry for 5+ years knows that there are certain features that we should have had several years ago. One of those features is the HTML5 placeholder; we used JavaScript shims for a decade before placeholder came...

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!