Files and Cache Expiration
For Apache to send along
Expires
with your staticly served files, you will need to make sure thatmod_expires
has been installed on your server. It is fairly standard and none of my servers I checked (that includes OS X’s Apache installation), didn’t havemod_expired
on the system. You may have to search through yourhttpd.conf
file and uncomment (remove the pound-signs) from the following lines, however:
#LoadModule expires_module libexec/httpd/mod_expires.so
#AddModule mod_expires.c
Once you’ve uncommented these lines, you should restart Apache. It differs depending on your system, but generally you can issue the following command:
sudo apachectl restart
If that didn’t work, look through your system’s manual or documentation for more information on how to restart Apache.
Once that’s done, go to the directory where your statically served files are contained and add or edit the .htaccess
file in that directory. Add the following lines:
<ifmodule mod_expires.c>
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>
The following bit: jpg|gif|png|css|js
specifies the file types you’d like to target and this other bit: ExpiresDefault "access plus 1 year"
specifies how long into the future you’d like to set the expiration date. In this case it’s one year into the future.
Leave a comment