Set expire headers in .htaccess
In order to activate the feature, you have to optimize your .htaccess file. In the case of Joomla, you need to make sure you have renamed htaccess.txt to .htaccess, and switch on URL-Rewriting in your Global Configuration. In case you use another CMS (including Wordpress), you will usually find a .htaccess file in the main root of your website. In the .htaccess file, we will first set some more rules that I will first explain shortly:
- ETag: Inform browser that image has already downloaded and should not be reloaded
- Expires headers: like ETag but more specific per file types
- AddOutputFilterByType DEFLATE: minifies HTML by removing blanks ()
The last rule does not actually control the caching of resources, but it takes care of compression for specified file types. Strictly speaking it does not belong in the Leverage browser caching chapter, but we might as well include it while we're updating the .htaccess file.
Then, we are going to specify our rules (expire headers). There are a lot of ways you can specify your rules, depending on what file-types you use on your site, and how long the expiry should be. A simple example to start off with could be the following:
You may need to further enhance the code and add expire headers for other file-types you use, like .mpeg, .woff, etc., or chance the expiry time if it does not suit your needs.
Note that your server needs to support the mod_expires feature in Apache. For safety, the code is wrapped in an if-statement. If your host does not support mod_expires, your site will crash. My own localhost environment on my PC also does not support it, so it helps me not having issues when I restore the site locally for testing purposes. With the if-statement in place, the code will simply not execute if no support is available.
For a more advanced example of a .htaccess file (including a lot of security rules), see the docs.joomla.org site.
Before you make any changes, first make a backup of your .htaccess file, and make sure to test thoroughly after activating the change. Check before and after to see if the change made a difference to the speed of your Joomla website.
Avoid browser caching in development