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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失