doumo2501 2016-04-04 16:39
浏览 74
已采纳

使用.htaccess进行文件缓存

A few months ago I looked around for some information regarding the best way to cache files (stylesheets, images) in our site. What I found was to add a few lines of code in the .htaccess file:

# Expires caching (Caching static files for longer drastically improves performance, you might even want to put even more aggressive times)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/html "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 1 month"
</IfModule>

The problem however is that whenever we update our stylesheet or replace some images on the site, the only way for the user to see them is to actually refresh the page. We have some banners that we no longer want to show and they keep appearing on the site until there is a manual refresh.

Is there a better way to control cache?

Thanks.

  • 写回答

1条回答 默认 最新

  • doufen3563 2016-04-04 17:04
    关注

    You could append a query string to the resource src whenever you update it. So, your style sheet reference might look like this:

    <link rel="stylesheet" type="text/css" href="theme.css">
    <img src="smiley.gif" alt="Smiley face">
    

    Then next time you update it, you could break the cache by changing the query string:

    <link rel="stylesheet" type="text/css" href="theme.css?version1.2">
    <img src="smiley.gif?version1.2" alt="Smiley face">
    

    This allows you to continue working with the same filename theme.css

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下