【问题标题】:Cache for images, php, js, and html缓存图像、php、js 和 html
【发布时间】:2011-12-30 02:26:56
【问题描述】:

我想缓存我的所有文件,但我不知道如何让它工作以便测试批准。我目前有

<meta http-equiv="Cache-Control" content="private" />
<meta http-equiv="Expires" content="86400000" />
<meta http-equiv="Cache-Control" content="max-age=86400000" />

我添加的最后一行只是为了测试是否过期和 max-age 会有所帮助(它没有)

我使用的是http://www.webpagetest.org/https://developers.google.com/pagespeed/#http://gtmetrix.com/

谁能简单地告诉我如何确保所有内容都被私下缓存?我检查了一堆其他页面,但没有人提供合法的 HTML 代码。请列出实际代码,不要只告诉我使用 Cache-Control 和 expires 以及我见过的所有其他网站使用。我真的需要示例代码才能理解。感谢您提前提供任何帮助。我也在使用 PHP,所以如果在 header() 中这样做,那也可以。

非常感谢

编辑:我也尝试使用 .htaccess 来做到这一点,但没有奏效。我不知道这是我的服务器的设置还是什么,但它并没有在测试中改变任何东西。

【问题讨论】:

    标签: php html caching browser-cache


    【解决方案1】:

    当您在 HTML 文档中指定过期时间时,它仅适用于实际文档。

    假设您有一个启用 mod_expires 的 Apache 网络服务器,您可以创建一个名为 .htaccess 的文件并包含以下内容

    ExpiresActive On
    ExpiresByType image/gif       86400000
    ExpiresByType image/png       86400000
    ExpiresByType image/jpg       86400000
    ExpiresByType image/jpeg      86400000
    ExpiresByType text/html       86400000
    ExpiresByType text/javascript 86400000
    ExpiresByType text/plain      86400000
    

    【讨论】:

    • 那行得通我没有启用 mod_expires 这就是它以前不起作用的原因。感谢您向我指出这一点
    【解决方案2】:

    您可以使用 .htaccess 来缓存您的文件。

        #cache html and htm files for one day  
    <FilesMatch ".(html|htm)$">  
    Header set Cache-Control "max-age=43200"  
    </FilesMatch>  
    
    #cache css, javascript and text files for one week  
    <FilesMatch ".(js|css|txt)$">  
    Header set Cache-Control "max-age=604800"  
    </FilesMatch>  
    
    #cache flash and images for one month  
    <FilesMatch ".(flv|swf|ico|gif|jpg|jpeg|png)$">  
    Header set Cache-Control "max-age=2592000"  
    </FilesMatch>  
    
    #disable cache for script files  
    <FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">  
    Header unset Cache-Control  
    </FilesMatch>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 2011-03-24
      • 2010-11-06
      • 2010-12-13
      相关资源
      最近更新 更多