Search code examples
regexapacheno-cache

Apache disable caching except jpe?g|png|gif|js|css


Here is how I enabled cache for images, JavaScript and CSS in Apache httpd.conf

<IfModule mod_expires.c>
    <FilesMatch "\.(jpe?g|png|gif|js|css)">
        ExpiresActive On 
        ExpiresDefault "access plus 7 day"
        FileETag None
    </FilesMatch>
</IfModule>

My question is how to form FilesMatch regex negative-assertions to get result no-cache for everything except images, JavaScript and CSS. Below does not work.

# DISABLE ALL CACHING EXCEPT IMAGES,JAVASCRIPT AND CSS
<FilesMatch "\.?!(jpe?g|png|gif|js|css)$">
    FileETag None

    <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 Note "CACHING IS DISABLED"
        Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    </IfModule>
</FilesMatch>

Solution

  • Untested, but I think this should be (?<!\.(html|htm|js|css|json))$. See the question I linked as a duplicate for more details.