Making sure mod_expires is enabled
mod_expires may not be enabled by default in your Apache install. On a Debian based system (e.g. Debian, Ubuntu and their derivitives) run the following command to enable mod_expires:
sudo a2enmod expires
The resulting output will look like this:
Enabling module expires. Run '/etc/init.d/apache2 restart' to activate new configuration!
Follow the instructions to restart Apache to enable mod_expires. If it had already been enabled you would instead see this:
Module expires already enabled
On a RHEL/CentOS based system, edit /etc/httpd/conf/httpd.conf and make sure the following line is not commented out, restarting Apache after making any changes.
LoadModule expires_module modules/mod_expires.so
Add any required rules to the .htaccess file in the website’s root directory. If there isn’t already an .htaccess file then create one.
The file should contain this line:
And then the necessary rules. To configure how long a file type should be cached for, add a line following this rule:
ExpiresByType [mime type here] "access plus [number] [timeframe]"
The timeframe should be one of years, months, weeks, days, hours, minutes or seconds.
Here’s an example from one of my sites:
As far as images are concerned, I create new files with unique names if they need to be changed.
By doing this with the expires rules I am able to get the browsers to do a high level of caching but still have them get fresh files if and when I need them to.