.htaccess设置静态资源缓存(即浏览器缓存)
2014-06-11 16:19
260 查看
在HTTP标头中为静态资源设置过期日期或最长存在时间,可指示浏览器从本地磁盘中加载以前下载的资源,而不是通过网络加载。这样, 网站加载速度会更快.
下面的代码都需要放到.htaccess中才能生效. 推荐设置过期时间为一个月, 即: max-age=2592000.
通过FilesMatch设置
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|css|js)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
通过mod_expires.c设置
<IfModule 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"
</IfModule>
如需针对具体的资源类型需要这样:
ExpiresByType image/gif "access plus 5 hours 3 minutes"
下面的代码都需要放到.htaccess中才能生效. 推荐设置过期时间为一个月, 即: max-age=2592000.
通过FilesMatch设置
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|css|js)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
通过mod_expires.c设置
<IfModule 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"
</IfModule>
如需针对具体的资源类型需要这样:
ExpiresByType image/gif "access plus 5 hours 3 minutes"
相关文章推荐
- 引用静态资源的url添加版本号,解决版本发布后的浏览器缓存有关问题
- nginx静态资源设置缓存的方法
- 1.设置某些资源(如图片,css,js)在浏览器缓存的时间:Filter
- 浏览器静态资源的版本控制新思路.强制更新指定资源缓存.的探讨
- [分享黑科技]纯js突破localstorage存储上线,远程抓取图片,并转码base64保存本地,最终实现整个网站所有静态资源离线到用户手机效果却不依赖浏览器的缓存机制,单页应用最新黑科技
- Nginx 静态资源缓存设置
- 浏览器静态资源的版本控制新思路.强制更新指定资源缓存.的探讨
- 静态的html页面想要设置使用浏览器缓存
- 浏览器静态资源的版本控制新思路.强制更新指定资源缓存.的探讨
- 2.设置某些资源(如图片,css,js)在浏览器缓存的时间:Filter配置
- 静态的html页面想要设置使用浏览器缓存
- tips 前端 阻止 浏览器缓存静态资源
- Filter(四)常见应用3---控制浏览器缓存一些静态资源
- 4Filter应用3控制浏览器缓存页面中的静态资源的过滤器
- PHP下利用header()函数设置浏览器缓存的代码
- Django实战(5):引入bootstrap,设置静态资源
- IE,FF客户端浏览器缓存机制 && 服务器设置的expires, Cache-Control,no-cache,no-store机制
- 静态资源本地缓存+静态资源延时加载
- nginx设置wordpress静态资源压缩和过期时间设置
- 怎么避免浏览器缓存静态文件(js/css/img)