htaccess设置静态资源缓存(即浏览器缓存),合理设置好htaccess可以有效的提高网站的访问效率。
在HTTP标头中为静态资源设置过期日期或最长存在时间,可指示浏览器从本地磁盘中加载以前下载的资源,而不是通过网络加载。这样, 网站加载速度会更快.
下面的代码都需要放到.htaccess中才能生效. 推荐设置过期时间为一个月, 即: max-age=2592000.
通过FilesMatch设置
Header set Cache-Control "max-age=2592000"
通过mod_expires.c设置
ExpiresActive On
ExpiresDefault "access plus 1 months"
ExpiresByType text/html "access plus 1 months"
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/jpeg "access plus 2 months"
ExpiresByType application/x-shockwave-flash "access plus 2 months"
ExpiresByType application/x-javascript "access plus 2 months"
如需针对具体的资源类型需要这样:
ExpiresByType image/gif "access plus 5 hours 3 minutes"
设置expires和max-age缓存兼容写法
Header set Cache-Control "max-age=604800, public"
Header set Cache-Control "max-age=18000, public, must-revalidate"
Header set Cache-Control "max-age=3600, must-revalidate"
-------------------------分割线-----------------------------------------
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType application/x-shockwave-flash A2592000
ExpiresByType text/css A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType text/javascript A2592000
ExpiresByType text/html A600
SetOutputFilter DEFLATE
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
Header unset Last-Modified
FileETag none