Search code examples
phpapache.htaccesscachingbrowser-cache

Cache some files, not the others


I have page with static content (.jpg) mixed with server side cached content (.jpg too).

<body>

// Static content, "never change"
<img src="my_static_image.jpg">

// This image change every 6 hours, its name will not change.
<img src="changing_image.jpg">

<body>

I would like to optimize loading and refresing only the content that change, not the rest. In my case, changing_image.jpg keep the same name but is re-generated every 6 hours on serveur side.


Solution

  • Found on http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html#Apache_htaccess_caching_code

    <FilesMatch "/static/"> <-- this is regex, you can match by filename rather than by extention
        Header set Cache-Control "max-age=29030400, public" <-- longer
    </FilesMatch>
    <FilesMatch "/changing/"> <-- this is regex, you can match by filename rather than by extention
        Header set Cache-Control "max-age=21600, public" <--6 hours in seconds
    </FilesMatch>