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
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

  • By
    jQuery Comment Preview

    I released a MooTools comment preview script yesterday and got numerous requests for a jQuery version. Ask and you shall receive! I'll use the exact same CSS and HTML as yesterday. The XHTML The CSS The jQuery JavaScript On the keypress and blur events, we validate and...

  • By
    Facebook Sliders With Mootools and CSS

    One of the great parts of being a developer that uses Facebook is that I can get some great ideas for progressive website enhancement. Facebook incorporates many advanced JavaScript and AJAX features: photo loads by left and right arrow, dropdown menus, modal windows, and...

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!