Home / Software / Apache / How can I set my images and css to be cached by browsers for better performance?
How can I set my images and css to be cached by browsers for better performance?
Last updated: 05/21/2012
In apache, using an .htaccess file, you can do this:
# 365 days <filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$"> Header set Cache-Control "max-age=31536000, public" </filesMatch>
# 3 days <filesMatch "\.(css|js)$"> Header set Cache-Control "max-age=259200, public" </filesMatch>
This will set the expiration on your images, flash and video files to 365 days, and your css and js files to 3 days. (max-age is in seconds)
3 days is a little short, but if you make changes to these files somewhat regularly, just bear in mind that you may have some time before all users see the changes.