Browser Caching Files with Apache
Browser Caching Files with Apache
Jason Jason Photo By Jason Jason, Feb 24, 2016

In continuing my efforts to improve SEO on a couple of my websites (Baker Street Systems and Utah Projectors), I've decided to implement file caching in Apache. This type of caching is ideal for these types of websites where the images, css, js, or any other kind of file doesn't change very much. Of course, during development of the websites, you'll be changing your css, js, and image files constantly so there is no need for file caching (in fact, it can end up being a headache!), but as soon as you launch your site to production you should be implementing some sort of file caching system.

First thing to do is to enable the caching module withing Apache.

sudo a2enmod expires

This next part can be done a number of ways (like through .htaccess files in the website code or in the global Apache config files), but I prefer to keep everything in the site-specific config file, i.e. /etc/apache2/sites-available/bakerstreetsystems.com.conf. Just open that file up and add the following inside the VirtualHost tag:

ExpiresActive on
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/css "access plus 1 days"
ExpiresByType text/javascript "access plus 1 days"
ExpiresByType application/x-javascript "access plus 1 days"
ExpiresByType text/xml "access plus 60 minutes"

You can, of course, change how long each type of file should be cached. If you ever need to update the local cached file, all you need to do is perform a hard refresh in your browser and/or clear the local cache in the browser.

References

http://stackoverflow.com/questions/447014/website-image-caching-with-apache

http://httpd.apache.org/docs/current/mod/mod_expires.html

http://metaskills.net/2006/02/19/how-to-control-browser-caching-with-apache-2/


Tags & Categories