How to configure ETag with browser caching

sam picture sam · May 17, 2016 · Viewed 20.5k times · Source

I’ve setup a browser cache for a static site via the .htaccess file by setting:

# BROWER CACHING - 1 Day for images
<filesMatch ".(jpg|jpeg|gif|ico)$">
Header set Cache-Control "max-age=86400, public"
</filesMatch>

I’m fine with these images having a 1 day cache, but the site changes frequently and thus I don’t want to cache the CSS and JS files.

I’ve read about ETag, which, as I understand, allows you to cache a file, but also set its creation date, so if it gets updated the next time a client visits the site, it will check if the creation date matches.

  1. Have I understood ETag correctly?
  2. How do I configure it? I’ve looked around, but couldn’t find any info on its configuration.

Answer

Ed-AITpro picture Ed-AITpro · May 25, 2016

You would use either FileETag MTime Size or Header unset Etag and FileEtag none. Do not use both (Create ETag and Remove ETag) and only choose which one works best on your particular server.

# Create the ETag (entity tag) response header field
FileETag MTime Size

or

# Remove the ETag (entity tag) response header field
Header unset ETag
FileETag none