Turn OFF Cache for specific file with Apache

后端 未结 1 1384
攒了一身酷
攒了一身酷 2021-01-01 17:11

I have a page on a site which uses random() twig, in Firefox and Chrome it is prevented from working because it gets cached as soon as the page loads.

相关标签:
1条回答
  • 2021-01-01 17:37

    Figured it out, to target a specific file (in this case index.php), add this code to the bottom of .htaccess

    <Files index.php>
    FileETag None
    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, 11 Jan 1984 05:00:00 GMT"
    </Files>
    

    Alternatively to target a specific selection of files, ie. I'd like to cache images but nothing else (files that match html, htm, js, css, php will not be cached):

    <filesMatch "\.(html|htm|js|css|php)$">
    FileETag None
    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, 11 Jan 1984 05:00:00 GMT"
    </filesMatch>
    

    To check the .htaccess was being read I entered a few lines of rubbish at the bottom, found out it wasn't being read, renamed it from htaccess to .htaccess and it worked.

    0 讨论(0)
提交回复
热议问题