Leverage Browser Caching

Leverage Browser Caching

This is a simple way to start caching in browsers and proxies, important to get some speed in your website, this is step one in our speed ideas

##### Comments, So we start with Mod expires put this in your htaccess #####
<IfModule mod_expires.c>
ExpiresActive On

# Images
ExpiresByType image/jpeg “access plus 1 year”
ExpiresByType image/jpg “access plus 1 year”
ExpiresByType image/gif “access plus 1 year”
ExpiresByType image/png “access plus 1 year”
ExpiresByType image/webp “access plus 1 year”
ExpiresByType image/svg+xml “access plus 1 year”
ExpiresByType image/x-icon “access plus 1 year”

# Video
ExpiresByType video/mp4 “access plus 1 year”
ExpiresByType video/mpeg “access plus 1 year”

# CSS, JavaScript
ExpiresByType text/css “access plus 1 year”
ExpiresByType text/javascript “access plus 1 year”
ExpiresByType application/javascript “access plus 1 year”

# Others
ExpiresByType application/pdf “access plus 1 year”
ExpiresByType application/x-shockwave-flash “access plus 1 year”
</IfModule>
##### Append any other type you use, first part finished here #####

# Comments next part we turn to Mod_Headers

# long time cache extensions first
<FilesMatch “\.(_otf|_ttf|asf|asx|avi|bmp|class|css|divx|doc|docx|eot|exe|gif|ico|jpe|jpeg|jpg|json|js|m4a|m4v|mdb|mid|midi|mov|mp3|mp4|mpe|mpeg|mpg|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|otf|pdf|png|pot|pps|ppt|pptx|qt|ra|ram|svg|svgz|swf|tar|tif|tiff|ttc|ttf|wav|wax|webm|webp|wma|wmv|wmx|woff|woff2|wri|xla|xls|xlsx|xlt|xlw|zip)$”>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
Header set Cache-Control “s-maxage=15768000, max-age=31536000, public, immutable”
# s-maxage = how long to live in a proxy
# max-age = how long to live in a browser
# public,immutable = store in cache, no changes will ever come
# that also means that changes to an image will never show, save as a new image instead, ok!  
Header unset Cookie
Header unset Set-Cookie
# Comments on unset cookie, you can try it, maybe makes no different but it dosen’t hurt to try 
Header unset Alt-Svc
# Comments om unset Alt-Svc, useful if you like to use some CDN, or remove
Header set Keep-Alive timeout=100,max=150
# comments on Keep-Alive, can be useful behind reverse proxies
</IfModule>
</FilesMatch>

# CAPITAL of the same
<FilesMatch “\.(_OTF|_TTF|ASF|ASX|AVI|BMP|CLASS|CSS|DIVX|DOC|DOCX|EOT|EXE|GIF|ICO|JPE|JPEG|JPG|JSON|JS|M4A|M4V|MDB|MID|MIDI|MOV|MP3|MP4|MPE|MPEG|MPG|MPP|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|OTF|PDF|PNG|POT|PPS|PPT|PPTX|QT|RA|RAM|SVG|SVGZ|SWF|TAR|TIF|TIFF|TTC|TTF|WAV|WAX|WEBM|WEBP|WMA|WMV|WMX|WOFF|WOFF2|WRI|XLA|XLS|XLSX|XLT|XLW|ZIP)$”>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
Header set Cache-Control “s-maxage=15768000, max-age=31536000, public, immutable”
Header unset Cookie
Header unset Set-Cookie
Header unset Alt-Svc
Header set Keep-Alive timeout=100,max=150
</IfModule>
</FilesMatch>

<FilesMatch “\.(html|htm|xsd|xsl|HTML|HTM|XSD|XSL|gz|gzip|GZ|GZIP)$”>
FileETag MTime Size
<IfModule mod_headers.c>
Header set Vary ‘Accept-Encoding, Cookie’
Header set Cache-Control “s-maxage=604800, max-age=60, proxy-revalidate, stale-if-error=900”
# Comments, proxy-revalidate = ask the proxy to look for changes
# stale-if-error = accept that the proxy serves an old verson if your webserver is uavalible, not all proxies support this but it dosen’t hurt to have it there!
# Ok this setup mean you can have a dynamic website, if you never make any updates set it the same as images instead!
Header set Content-Language “en”
</IfModule>
</FilesMatch>

<FilesMatch “\.(txt|xml|TXT|XML)$”>
FileETag MTime Size
<IfModule mod_headers.c>
Header set Cache-Control “s-maxage=600, max-age=3”
Header set Content-Language “en”
</IfModule>
</FilesMatch>

# Compress the types you like, not images
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
</IfModule>

# MY extra if served as pre compessed
<IfModule mod_mime.c>
<FilesMatch “\.html\.gz$”>
ForceType text/html
</FilesMatch>
AddEncoding gzip .gz
AddType text/html .gz
</IfModule>
<IfModule mod_deflate.c>
SetEnvIfNoCase Request_URI \.gz$ no-gzip
</IfModule>
# End

Ok as you understad you can, and should remove my comments, please contribute with better ideas

Download as text file


Activities (2) Benidorm (1) browsers (1) Cache (3) CDN (2) Cloudflare (2) htaccess (1) privacy (1) security (1) Speed (3) Sporting (1) Walk (1) Windows (1) WordPress (2) WP plugins (1)