I’m working on a WordPress site which was installed on a fairly slow server so I have to deal with even the slightest speed issues I find. The first thing I’ve tried was W3 Total Cache which is rather a bloatware than a performance optimization plugin (It might be good for better hosts but not for most of the shared hosts). Few more cache plugins later I’ve decided to set up the easiest and the fastest cache and compression I can imagine (via htaccess). This article is useful for WordPress owners but also for anyone who uses PHP and Apache.
Always backup! Seriously, before doing anything please backup in case of anything goes wrong. Even the professionals can make mistakes.
The cache solution is pretty easy but the result is quite conspicuous. The first thing I’ve done is extension specific cache control.This means that the browser will keep the files in local cache for specific period of time and will serve them from locally. (I’ve put the following lines into the .htaccess in the WordPress root but you can put wherever you need.)
# Set cache for static files. Cached for 30 days <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=2592000, public, must-revalidate" <FilesMatch>
You can remove or add extensions in the second line by changing the regular expression. If you want to decrease or increase cache time you can edit the number 2592000 (60^2*24*30). This number indicates the seconds within the file could be served from local cache if it’s already cached. (For example if you want 7 days cache you can use 604800.) If you want to add different cache time for sets of extensions you can do that as the following example presents.
# Images cached for 7 days <FilesMatch "\.(ico| jpg|jpeg|png|gif)$"> Header set Cache-Control "max-age=604800, public, must-revalidate" </FilesMatch> # JS files cached for 14 days <FilesMatch "\.js$"> Header set Cache-Control "max-age=1209600, public, must-revalidate" </FilesMatch>
While max-age affects the speed only after the first page load, Gzip can improve performance at the very beginning. The easiest way to use Gzip compression for every possible http data transmission you better put the related code into your .htaccess file. (Note that you server may won’t let you use Gzip plus some old browser can’t decompress Gzip data.)
You can check if the htaccess modifications are working if you open the network tab on developer console in your browser (eg: Chrome -> F12), find the target file that you want to check then open the information (mostly by click on it). When you see the http informations look for “Cache-Control” (in response headers) and check if the value equals to that you’ve set. If you added Gzip compression you should see “Content-Encoding: gzip” in response headers list otherwise it might be unsupported by your host.
If you got Internal Server Error (500) then you better use your backup. This error can be caused by old Apache version or invalid htaccess contents.
I hope this article helps you a little and if you have any problem make yourself at home in the comment section. Also if you have any suggestions to improve the code examples in the article your reply is welcomed. Click here to download WordPress root htaccess example file.