Simple WordPress Speed Optimisation

WordPress is a great piece of blogging software. With very little experience you can have your own blog set up within minutes. However, WordPress does have its faults. One common complaint is that WordPress is slow, and gets slower as more posts are added. Fear not, because help is at hand! One simple change to your .htaccess file can optimise WordPress and speed your blog up dramatically.

Background

The default WordPress .htaccess file looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

The URL rewriting code is needed for permalinks, but it is inefficient. With a little optimisation we can speed WordPress up a great deal.

The default WordPress .htaccess file will intercept every single request made by a visitor’s web browser, whether it be for a page, an image file, a JavaScript file or anything else, and make Apache check whether the requested resource exists as a file or a directory. If it does then it is served straight to the visitor, if it does not exist then WordPress takes over and searches for a post that matches the requested resource.

The Problem

The default WordPress .htaccess setup is not efficient because it forces Apache to check whether every single requested resource is a real file or directory that exists on the server. If you’ve ever looked at the source code of a WordPress template you’ll see that they include multiple CSS files, JavaScript files and images.

When a visitor loads your blog their web browser requests all of these files and Apache has to check whether each one of them exists before either serving them or letting WordPress take over. This is very inefficient and can be a major cause of WordPress blogs running slowly.

The Solution

In a nutshell you need to modify your .htaccess file so that the existence of certain file types such as CSS, JavaScript and images will not be checked by Apache. We know that these resources will be real files and so there is no need to check for their existence because WordPress is never going to handle those requests.

Replace the WordPress portion of your .htaccess file with the following:

# BEGIN WordPress
#
RewriteEngine on
#
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive
# RewriteBase /
#
# If this request is for "/" or has already been rewritten to WordPress
RewriteCond $1 ^(index\.php)?$ [OR]
# Or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|ico|css|js)$ [NC,OR]
# Or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# Or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# Then skip the rewrite to WordPress
RewriteRule ^(.*)$ - [S=1]
# Else rewrite the request to WordPress
RewriteRule . /index.php [L]
#
# END WordPress

As the comments explain, this simply tells Apache that it doesn’t have to check for the existence of the file types listed.

An Admission

I must admit that I can’t take credit for this optimisation. I stumbled across it in a topic posted on WebmasterWorld by one of the administrators, jdMorgan. In that topic a twofold .htaccess speed increase was mentioned, so it’s unclear how that would translate to a WordPress blog overall. Overall speed increases would depend a lot on the plugins installed, as a slow or badly optimised plugin could slow a blog right down.

This Optimisation in Action

I was working with a WordPress blog that was taking up to 6 seconds to load when checked with a website load time checker. The blog in question had a custom theme and a number of plugins, which meant lots of CSS and JavaScript files in the head, as well as a generous number of images. After making the optimisation detailed above the blog load time dropped consistently to around 0.9 seconds!

Conclusion

I won’t claim that this WordPress optimisation will speed up your blog’s load time by 80% like it did for me, but with gains like that to be made I feel it’s certainly worth bringing to your attention!

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *