
Modern browsers will cache downloaded content for a period of time, so as to not be downloading the same content repeatedly. These browsers will keep this cache for a few days by default, or until the user clears it manually. However, if the visitor visits your site frequently, it would be more beneficial for the browser to cache this content for a longer amount of time.
That’s where this article come into play. It is possible to send HTTP response headers that tell the browser to cache the content for (up to) a longer amount of time. For the visitors who visit your site frequently, this will benefit both you and them. Since they won’t be repeatedly downloading the same content, their page will load slower, and your server will consume less bandwidth. For the visitors who don’t return to your site, telling the browser to cache the content for longer really has little negative effect.
The response headers we’ll be setting are Expires and Cache-Control. Assuming you have already installed mod_headers, this will be a snap. Start by logging into your server via SSH, and opening your Apache configuration file (/usr/local/apache2/conf/httpd.conf).
If you followed our GZIP article last week, then you’ll already have part of this code in your file. Find that code, and add this above it:
ExpiresDefault "access plus 2 weeks"
ExpiresActive On
Header append Cache-Control "public"
Assuming you did follow last week article, then immediately after this line:
Add these lines:
ExpiresDefault "access plus 1 months"
ExpiresActive On
Header append Cache-Control "public"
After you have add those two snippets, save and close the file and then restart Apache. This will tell browsers to cache .css files for two weeks and images for one month. While you could certainly cache CSS files for one month, I, personally, tweak my CSS files more frequently than I change images. Feel free to change that to suit your own needs though.




