在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"

相关文章:

  • 2021-11-14
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-03-09
猜你喜欢
  • 2021-05-21
  • 2021-06-01
  • 2021-10-11
  • 2022-01-11
  • 2021-07-15
  • 2022-12-23
  • 2021-11-12
相关资源
相似解决方案