2011年4月1日 星期五

.htaccess 快取網站圖片, js , CSS , png ,gif

利用 .htaccess 來達成 圖片與js 檔案 304 cache .


# 快取一年
<FilesMatch "\.(ico|pdf|flv)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>
# 快取一周
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 快取二天
<FilesMatch "\.(xml|txt|css|js)$">
Header set Cache-Control "max-age=172800, proxy-revalidate"
</FilesMatch>
# 快取一分鐘
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>
#FileETag MTime Size mod_expires 可以指定副檔名- 用以下兩個方式, 當檔案有改變時,會再重新讀取. <ifmodule mod_expires.c> <filesmatch "\.(jpg|gif|png|css|js)$"> ExpiresActive on ExpiresDefault "access plus 1 year" </filesmatch> </ifmodule> # Requires mod_expires to be enabled. 不只定副檔名 <IfModule mod_expires.c> # Enable expirations. ExpiresActive On # Cache all files for 2 weeks after access (A). ExpiresDefault A1209600 # Do not cache dynamically generated pages. ExpiresByType text/html A1 </IfModule>


很詳細的說明: http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/
參考網站 :http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html

【下列文章您可能也有興趣】

沒有留言: