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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败