如何防止网站内容的缓存?

时间:2012-05-24 10:16:36

标签: javascript html apache .htaccess

我有一个动态网页,其中一小部分内容会一直在变化。

每个请求的不同内容由以下内容组成:Javascript和HTML

为了使网站正确显示,javascript和HTML需要100%全新服务器。我检查了那些不时出现的页面错误是旧的/以前加载的javascript或HTML而不是来自服务器的新数据的结果。

我在.htacces中绑定了以下设置:

Header Set Cache-Control "max-age=0, no-store"

上述设置正在运行,但问题是每次都需要重新加载图像,这是不必要的,对网站性能不利。

我在.htacces中尝试了以下设置:

### turn on the Expires engine
ExpiresActive On

### expires after a month in the client's cache
ExpiresByType image/gif A36000
ExpiresByType image/png A36000
ExpiresByType image/jpg A36000
ExpiresByType image/x-icon A36000
ExpiresByType application/pdf A36000
### ExpiresByType application/x-javascript A36000
### ExpiresByType text/plain A36000

但是上面似乎没有用,因为我检查的内容并不总是新鲜的,导致页面错误。

我的问题:

如何正确配置服务器,以便始终(通过php脚本生成)使用新的HTML和javascript?

2 个答案:

答案 0 :(得分:1)

text / html和css文件与图像的过期时间有何不同?

ExpiresByType text/html "access plus 5 seconds"
ExpiresByType text/css "access plus 5 seconds"
ExpiresByType image/jpg "access plus 5 minutes"
etc

答案 1 :(得分:0)

在我的应用程序具有相同的更新要求的情况下,我使用了以下(php):

// disable cache (ajax useful)
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");