Last Updated: February 25, 2016
· pimschaaf

Pagespeed: Leverage browser caching through FilesMatch

As an alternative to browser caching through Apache's modexpires, use FilesMatch (e.g. in cases where modexpires can't be enabled).

Firstly, disable ETag headers. By removing the ETag header, you disable caches and browsers from being able to validate files, so they are forced to rely on your Cache-Control and Expires header.
Secondly, match mime-types and set Cache-Control and Expires headers.

In .htaccess:

# ----------------------------------------------------------------------
# | ETags                                                              |
# ----------------------------------------------------------------------

# Remove `ETags` as resources are sent with far-future expires headers.

# `FileETag None` doesn't work in all cases.
<IfModule mod_headers.c>
    Header unset ETag

FileETag None

# 1 YEAR
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
Header set Cache-Control "max-age=31536000, public"

# ~ 1 MONTH
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=2629743, public"

# 1 WEEK
<FilesMatch "\.(txt|xml|js|css)$">
    Header set Cache-Control "max-age=604800, public"

# NEVER CACHE - notice the extra directives
<FilesMatch "\.(html|htm|php|cgi|pl)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"