Apache caching files
Caching files is one of the most important aspects of web development in regards to having a fast downloading site. Correctly caching files can result in users limiting the amount of files downloaded and using the ones stored in their browser instead.
The major goal of apache caching is to send the file to the browser and request that the browser stores the file for an extended period of time without requesting it again.
To do this we must first setup our apache server and disable anything that allows browsers to set there own caching mechanisms. The two attributes that contribute to browser caching are e-tags and last modified headers:
### turn off etags ###
Header unset ETag
FileETag None
### turn off last modified headers ###
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
</FilesMatch>
Now that we have disabled everything, browsers do not know how to cache files as we have given them no information that they can use for caching. Therefore we can now proceed to give our own correct caching information in which the browser must accept and go by.
### cache our files ###
# 1 YEAR
<FilesMatch "\.(pdf|flv)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>
#1 YEAR
<FilesMatch "\.(css|js)$">
Header set Cache-Control "max-age=29030400, proxy-revalidate"
</FilesMatch>
# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf|ico)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 2 DAYS
<FilesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=172800, proxy-revalidate"
</FilesMatch>
# 1 MIN
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>
Our files should now be cached for our specified times and browsers should not even request the new files until the time has expired.