Cache Busting

There is a widely used module available that will handle cache busting out of the box.

https://github.com/gordonknoppe/magento-cachebuster

Quote

The module provides cachebusting by automatically altering the URI created by Magento for static files by adding the timestamp of the file to the filename:

Before: http://www.example.com/js/varien/js.js

After: http://www.example.com/js/varien/js.1324429472.js

Server Config

For the cache busted files to be resolved by your web server you need to update your relevant server config.

Nginx

Inside your main server block (behind Varnish if applicable), add the following.

1
2
3
location ~* (.+)\.(\d+)\.(js|css|png|jpg|jpeg|gif)$ {
        try_files $uri $1.$3;
}

If there are file extensions missing from the regex then add them in using a pipe i.e: (...|svg)

Apache

Add the following to your .htaccess or virtualhost definition. This is taken directly from the github page (untested).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<IfModule mod_rewrite.c>

############################################
## rewrite files for magento cachebuster

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

</IfModule>