How to Enable Caching from htaccess

Allowing your visitors to cache objects from your site can vastly improve their load-time and also reduce your server load.

Some browsers may not cache files if the files don’t have an expires header. To quickly and easily enable sitewide caching, you can use your htaccess to tell your server to include the appropriate headers on cacheable content.

<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresActive on
ExpiresDefault "access plus 7 days"
Header unset ETag
FileETag None
</FilesMatch>

This is an example section of an htaccess file that enables caching. For all files that match those filetypes (jpg, png, css, etc.) your server will send an expires header of seven days after accessed.

The ETag is an optional header for cache validation. If an ETag is blocked (as it is in the example above) the cached versions of files will be served even if the source files have changed. Using an ETag, the client can check if files have changed before deciding to serve the cached version of a file or reloading the file from the server. The ETag value changes whenever the content of a URL changes – thus the cache will never serve stale content.

Leave a Reply