如何使用.htaccess阻止PHP缓存

时间:2015-07-07 16:08:57

标签: php .htaccess

我使用以下代码来缓存未知和已知文件,但我不希望缓存PHP,因为它导致动态脚本出现问题。注意我不想为每个PHP文件添加标头以避免它被缓存。如果可以防止PHP被缓存,请建议我。

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 1 year"
    </IfModule>

2 个答案:

答案 0 :(得分:2)

应同时适用于.htaccesshttpd.conf

<filesMatch "\.(html|htm|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 Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>

答案 1 :(得分:1)

感谢它对我有用我只是将其定制为

<filesMatch "\.(html|htm|php)$">
 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 Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>