指定静态文件缓存的到期日期



当我测试我的网站的SpeedTest我看到很多过期未指定错误。你可以在这一页看到。

我将这段代码添加到。htaccess文件

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

但一切都没有改变。你能给我个建议吗,我可以为我的静态文件指定截止日期吗?

服务器:Linux - Apache

我喜欢html5boilerplate的方式:

<IfModule mod_expires.c>
  ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"
# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest       "access plus 0 seconds"
# your document html 
  ExpiresByType text/html                 "access plus 0 seconds"
# data
  ExpiresByType text/xml                  "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType application/json          "access plus 0 seconds"
# rss feed
  ExpiresByType application/rss+xml       "access plus 1 hour"
# favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week" 
# media: images, video, audio
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
  ExpiresByType image/jpg                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType video/ogg                 "access plus 1 month"
  ExpiresByType audio/ogg                 "access plus 1 month"
  ExpiresByType video/mp4                 "access plus 1 month"
  ExpiresByType video/webm                "access plus 1 month"
# htc files  (css3pie)
  ExpiresByType text/x-component          "access plus 1 month"
# webfonts
  ExpiresByType font/truetype             "access plus 1 month"
  ExpiresByType font/opentype             "access plus 1 month"
  ExpiresByType application/x-font-woff   "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# css and javascript
  ExpiresByType text/css                  "access plus 2 months"
  ExpiresByType application/javascript    "access plus 2 months"
  ExpiresByType text/javascript           "access plus 2 months"
  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>
</IfModule>

希望这对你有用。来源:https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

您可以使用mod_headers来完成此操作:

<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
  Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

或者你可以使用mod_expires:

ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"

我看到你已经安装了PHP(参考wordpress),所以我们将尝试诊断你的问题:

    重新启动apache web服务器。如果您拥有apache服务器(命令行访问),您可以这样做。
  1. 创建phpinfo页面这将允许您查看已安装的apache模块。检查您要使用的mod_expires.c模块(应该能够在浏览器中为mod_expires执行CTL-F(查找)请求)。如果你找到了——太好了!如果没有,您需要在apache服务器上安装mod_expires。如果您拥有自己的apache服务器,您可以搜索如何为您的特定操作系统安装模块。如果没有,您可以向您的主机提供商查询如何安装此模块。安装完成后,继续。
  2. 修改http.conf条目,在表达式
  3. 中包含"plus"
  4. 重启Apache
  5. 测试=)

你只需要添加:

ExpiresByType application/javascript "access plus 1 year"

也许你没有加载mod_expires,所以整个节是一个no-op。或者,AllowOverride为None,则不处理。htaccess文件。

相关内容

  • 没有找到相关文章

最新更新