Faster website in one minute without any plugin

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.

Benefits of browser cache

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>

Advantages of Gzip

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.)

#Gzip output for JavaScript files
<IfModule mod_version.c>
  <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE text/javascript application/javascript
  </IfModule>
</IfModule>

The code from above will force the server to compress all JavaScript files before sending it to the client. Of course you can add your own mime types after application/javascript. Additional mime types has to be separated with spaces. (Here is a list for lot of mime types which you can use for your own build.) Please keep in mind that you should not waste resources on already compressed formats like JPG, Zip etc.

How can I test the results?

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.

Developer console info

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.

Conclusion

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.

No comments yet. - Start the discussion

There are no comments yet. You can change this. It’s 100% up to you.

Participate

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