WordPress .htaccess for Performance

WordPress-LogoWordPress isn’t known for being the fastest CMS out there – but there are a few easy tricks that you can do with your .htaccess file that can significantly increase performance. Recently I launched a WordPress site and web to Web Page Test to evaluate the performance. Before I did anything the site had failing marks on every single category – but after changing up the .htaccess file it only failed two categories and both of these were due to plugin scripts.

Below is a copy of the optimized for performance .htaccess file for a WordPress site:

We can go through a quick rundown of the file now as well. The first section is just WordPress stuff so there isn’t any performance going on there. Next we have the gzip compression settings from line 12 to line 22. Compression allows your web server to provide smaller files that load faster for visitors to your website. When a request is made by a browser for a page from your site the server quickly compresses the files needed, and then sends the smaller compressed file to the browser which then unpacks it. The compression and decompression steps take an extremely small amount of time, which means that it takes less time to load your pages. All modern browsers understand and accept compressed files so there’s no need to worry about that.

Next from lines 24 to line 58 we set headers that our max-age and expires directives are set. This sets up everything we need to cache files in the browser. The cache is a place data is stored that allows it’s quick retrieval later. For example, if you already loaded an logo image from a website it would be really great if that was stored in the cache so it could be easily retrieved. Since the logo is displayed on every page (most likely) this will end up saving a lot of time and increasing site performance. That’s pretty much what we’re trying to do here; making sure icons, images, flash files, JavaScript, HTML and XHTML files are all cached properly.

Finally we set the keep-alive directive. Keep alive is a method to allow the same tcp connection for HTTP conversation instead of opening new one with each new request. This is actually best done using your Apache configuration file with the following:

However – I am aware that not everyone will have access to that. The solution presented in the .htaccess file line 60 to line 63 will do close to the same thing but without needing to change any configuration settings on your server.

Hopefully this gives everyone reading a quick insight into some performance improvements that can be made. Performance is becoming more and more important with the rise of mobile browsing so I think its good to keep in the back of your head when developing. Little tricks like this will work but the best solution for performance is to develop with performance in mind from the start of a project.

As always thank you for reading and please share it around as much as you can! Please feel free to put any suggestions or ideas for future tutorials in the comments section below.