2011-12-30 51 views
2

我想緩存我的所有文件,但我無法弄清楚如何讓它工作以便測試通過。我目前緩存圖片,php,js和html

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

最後一行我說只是爲了測試是否有過期和最大年齡將有助於(事實並非如此)

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

任何人都可以告訴我如何確保一切都是私人緩存?我檢查了一堆其他頁面,但沒有人提供合法的HTML代碼。請列出實際的代碼不只是告訴我使用緩存控制和過期,並像我見過的每個其他網站使用。我真的需要示例代碼才能理解。感謝您提前提供任何幫助。我也使用PHP,所以如果它在一個頭(),那麼這也可以。

非常感謝您

編輯:我爲了做它用的.htaccess也嘗試過,但沒有奏效。我不知道這是我的服務器的設置還是什麼,但它沒有改變任何測試。

回答

5

當您在HTML文檔中指定過期時間時,它僅適用於實際文檔。

假設你有一個Apache網絡服務器與mod_expires啓用,您可以創建一個名爲.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 
+0

工作我沒有啓用mod_expires這就是爲什麼它不工作之前。謝謝你指出我 – eric 2011-12-30 03:03:53

4

可以用.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>