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
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

Incredible Demos

  • By
    RealTime Stock Quotes with MooTools Request.Stocks and YQL

    It goes without saying but MooTools' inheritance pattern allows for creation of small, simple classes that possess immense power.  One example of that power is a class that inherits from Request, Request.JSON, and Request.JSONP:  Request.Stocks.  Created by Enrique Erne, this great MooTools class acts as...

  • By
    MooTools ContextMenu Plugin

    ContextMenu is a highly customizable, compact context menu script written with CSS, XHTML, and the MooTools JavaScript framework. ContextMenu allows you to offer stylish, functional context menus on your website. The XHTML Menu Use a list of menu items with one link per item. The...

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!