Cache for images, php, js, and html

2019-04-02 05:53发布

I want to cache all of my files but I can't figure out how to get it to work so that the tests approve. I have currently

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

The last line I added just to test if having expires and max-age will help (it doesn't)

I was using http://www.webpagetest.org/, https://developers.google.com/pagespeed/#, and http://gtmetrix.com/

can anyone tell me simply how to make sure everything is cached privately? I have checked a bunch of other pages but no one gives legit HTML code. Please list actual code don't just tell me to use Cache-Control and expires and that like every other website I have seen uses. I really need example code in order to understand. Thank you for any help in advance. I am also using PHP so if doing it in a header() then that would work too.

Thank you very much

edit: I also tried using .htaccess in order to do it but that didn't work. I don't know if it was a setting with my server or what but it didn't change anything with the test.

2条回答
Emotional °昔
2楼-- · 2019-04-02 06:02

you can use .htaccess to cache your files.

    #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>  
查看更多
相关推荐>>
3楼-- · 2019-04-02 06:13

When you specify an expiration time in an HTML document, it only applies to the actual document.

Assuming you have an Apache webserver with mod_expires enabled, you can create a file named .htaccess and include the following

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
查看更多
登录 后发表回答