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.
#
# https://developer.yahoo.com/performance/rules.html#etags
# https://tools.ietf.org/html/rfc7232#section-2.3
# `FileETag None` doesn't work in all cases.
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
# 1 YEAR
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
# ~ 1 MONTH
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=2629743, public"
</FilesMatch>
# 1 WEEK
<FilesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 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"
</FilesMatch>
Written by pimschaaf
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Apache
Authors
nekwebdev
47.02K
petenicholls
33.47K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#