Skip to content

Instantly share code, notes, and snippets.

@notfalsedev
Last active March 7, 2018 07:56
Show Gist options
  • Save notfalsedev/d87b8040055a684ec79443b8fe2eef65 to your computer and use it in GitHub Desktop.
Save notfalsedev/d87b8040055a684ec79443b8fe2eef65 to your computer and use it in GitHub Desktop.
Handle Chrome/Firefox cache for CSS and JS files
# Block libwww-perl
SetEnvIfNoCase User-Agent "^libwww-perl*" block_bad_bots
Deny from env=block_bad_bots
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
RewriteEngine On
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Force www
RewriteCond %{HTTP_HOST} ^%{HTTP_HOST}% [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%/$1 [L,R=301,NC]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Fix the css and js caching with versioning
RewriteRule ^(.*)/(.+)\.v(.+)\.(js|css)$ $1/$2.$4 [L]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
<IfModule mod_expires.c>
# Enable expirations.
ExpiresActive On
# Cache all files for 2 weeks after access (A).
ExpiresDefault A1209600
# Enable Deflate
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>
# Enable HSTS
<IfModule mod_headers.c>
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" env=HTTPS
</IfModule>
# Disable HTML cache
<FilesMatch "\.(html)$">
ExpiresActive Off
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 08 Jan 1975 05:00:00 GMT"
</IfModule>
</FilesMatch>
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</FilesMatch>
# Set up caching on media files for 1 week
<FilesMatch "\.(gif|jpg|jpeg|png|swf|bmp)$">
ExpiresDefault A29030400
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</FilesMatch>
# Set up 2 Hour caching on commonly updated files
<FilesMatch "\.(xml|txt|js|css)$">
ExpiresDefault A29030400
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</FilesMatch>
</IfModule>
@notfalsedev
Copy link
Author

notfalsedev commented Mar 7, 2018

<link rel="stylesheet" type="text/css" href="/dist/style.v1.css" />
<script type="application/javascript" src="/dist/script.v1.js"></script>

Just crank up the v-number in the filename to release a new CSS/JS file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment