Skip to content

Instantly share code, notes, and snippets.

@davidpede
Last active February 5, 2025 16:26
Show Gist options
  • Save davidpede/e8980b6240a63b4bfc7456269643172a to your computer and use it in GitHub Desktop.
Save davidpede/e8980b6240a63b4bfc7456269643172a to your computer and use it in GitHub Desktop.
File caching using .htaccess
RewriteEngine On
RewriteBase /
### ---REWRITE DYNAMIC CACHE VERSION URL--- ###
# EXAMPLE: http://domain.com/css/prp-styles_ver<timestamp>.css
# RewriteRule (.*)_ver\d+\.(.*)$ $1.$2 [L]
### ---REWRITE DYNAMIC CACHE VERSION URL--- ###
### ---CACHE HEADER CONTROL--- ###
# EXPIRES HEADER #
ExpiresActive On
# 1 MONTH
<FilesMatch "\.(gif|png|jpg|jpeg)$">
Header set Cache-Control "max-age=2419200"
</FilesMatch>
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
### ---CACHE HEADER CONTROL--- ###
# 1 HOUR: 3600
# 1 DAY: 86400
# 1 WEEK: 604800
# 1 MONTH: 2419200
# 1 YEAR: 31536000
## EXPIRES CACHING ##
#<IfModule mod_expires.c>
#ExpiresActive On
#ExpiresByType image/jpg "access 1 week"
#ExpiresByType image/jpeg "access 1 week"
#ExpiresByType image/gif "access 1 week"
#ExpiresByType image/png "access 1 week"
#ExpiresByType text/css "access 1 week"
#ExpiresByType application/pdf "access 1 week"
#ExpiresByType text/x-javascript "access 1 week"
#ExpiresByType application/x-shockwave-flash "access 1 week"
#ExpiresByType image/x-icon "access 1 week"
#ExpiresDefault "access 1 week"
#</IfModule>
#<IfModule mod_expires.c>
#ExpiresActive On
#ExpiresByType image/gif A2592000
#ExpiresByType image/jpeg A2592000
#ExpiresByType image/png A2592000
#ExpiresByType image/ico A2592000
#ExpiresByType image/x-icon A2592000
#ExpiresByType text/css A2592000
#ExpiresByType application/javascript A2592000
#ExpiresByType application/x-javascript A2592000
#</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment