缓存图像,php, js和html


Cache for images, php, js, and html

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

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

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

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

谁能告诉我简单地如何确保一切都是私下缓存?我检查了一堆其他页面,但没有人给出合法的HTML代码。请列出实际的代码不只是告诉我使用缓存控制和过期,就像我看到的其他网站一样。我真的需要示例代码来理解。提前感谢您的帮助。我也使用PHP,所以如果在header()中这样做,那么也可以工作。

Thank you very much

编辑:我也尝试使用。htaccess,以做到这一点,但没有工作。我不知道这是我的服务器设置还是什么,但它没有改变任何测试

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

假设您有一个启用了mod_expires的Apache web服务器,您可以创建一个名为.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

你可以使用。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>