htaccess - filename-based cache busting

If you use far-future expires headers not using the build script to manage your filename version revving, you might want to consider enabling this, which will  route all requests from /path/filename.20120101.ext to /path/filename.ext.

Traditionally, if you use a far future Expires header you have to change the component's filename whenever the component changes.

To use this, just add a time-stamp number (or your own numbered versioning system) into your resource filenames in your HTML source whenever you update those resources.

Examples:

<script type="text/javascript" src="/js/script.20130702.js">{/script>
<script type="text/javascript" src="/js/jqueryplugin.45.js">{/script>
<link rel="stylesheet" href="css/somestyle.49559939932.css">
<link rel="stylesheet" href="css/anotherstyle.2.css">

 

To enable this, paste this code into your .htaccess file

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
{/IfModule>

 

What about file?v=1231 ?

Revving Filenames: don’t use querystring, the query string approach is not reliable for clients behind a Squid Proxy Server. The proxy server can be configured to work with this form of cachebusting, but its default configuration is unreliable for this method. Since Squid Proxy version 2.7 (released May 31 2008) the change is no longer necessary as described in Squid Wiki: Caching Dynamic Content.

Bottom line: Query string cachebusting is not guaranteed to work for all your visitors.

Far-future exipres headers

This makes your website is more cached on the client side, making it faster to browse.

Warning: The code assume you control versioning with filename-based cache busting like above

In .htaccess add:

<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 month"
ExpiresByType text/cache-manifest "access plus 0 seconds"
ExpiresByType text/html "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType image/x-icon "access plus 1 week"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
ExpiresByType text/x-component "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
{/IfModule>

That's all.