Create a file in the Apache configuration directory and include the below contents:
# cd /etc/httpd/conf.d/ # vi cache.conf
Add the following contents to the file.
<IfModule mod_headers.c> <FilesMatch ".(flv|swf|gif|jpg|jpeg|png|ico|js|css|pdf|txt)$"> Header set Cache-Control "max-age=604800" </FilesMatch> </IfModule>
Note: This is configured to serve all the listed file types with an expiry of one week (the minimum recommended value). You can tweak the age value and also split the file types out into different sections if you wish to be more granular. We also choose not to cache any HTML or PHP files due to the dynamic nature.
Once you have saved the contents of the file, confirm the Apache configuration then restart the service.
# apachectl -t Syntax OK # service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
Don’t forget to check your site for any issues that this may raise (although unlikely) and also test the speed benefit achieved from this change.
Should you need to force any resource to be updated for all users, regardless of the caching settings, the best way to do this is to create a resource with a new name and reference that. If you wish to force an update to occur only for your local browser then you can simply complete a “hard refresh” within your browser.