首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加Expires标头

添加Expires标头
EN

Stack Overflow用户
提问于 2012-12-20 20:51:30
回答 5查看 130.7K关注 0票数 52
代码语言:javascript
复制
Add Expires headers
There are 21 static components without a far-future expiration date.

    http://static.doers.lk/examples-offline.css
    http://static.doers.lk/kendo.common.min.css
    http://static.doers.lk/kendo.default.min.css
    http://static.doers.lk/style.css
    http://static.doers.lk/jquery.min.js
    http://static.doers.lk/kendo.web.min.js
    http://static.doers.lk/console.js
    http://static.doers.lk/sprite.png
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    http://static.doers.lk/favicon.PNG

当使用yahoo YSLOW测试网站时,它会说上面的消息。所以我不知道如何添加Expire头。如有任何帮助,我将不胜感激。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-12-20 20:54:05

添加这些头文件的最简单方法是一个.htaccess文件,该文件向您的服务器添加一些配置。如果资产托管在您无法控制的服务器上,那么您对此无能为力。

请注意,一些主机提供商将不允许您使用.htaccess文件,因此,如果他们的条款似乎不起作用,请检查他们。

HTML5Boilerplate项目有一个出色的.htaccess文件,它涵盖了必要的设置。在其Github repository上查看文件的相关部分

这些是重要的部分

代码语言:javascript
复制
# ----------------------------------------------------------------------
# Expires headers (for better cache control)
# ----------------------------------------------------------------------

# These are pretty far-future expires headers.
# They assume you control versioning with filename-based cache busting
# Additionally, consider that outdated proxies may miscache
# www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/

# If you don't use filenames to version, lower the CSS and JS to something like
# "access plus 1 week".

<IfModule mod_expires.c>
  ExpiresActive on

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Media: images, video, audio
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# CSS and JavaScript
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/css "access plus 1 year"
</IfModule>

他们有documented what that file does,最重要的一点是,当你的CSS和Javascript文件改变时,你需要重命名,因为你的访问者的浏览器一年内都不会再检查它们,一旦它们被缓存。

票数 73
EN

Stack Overflow用户

发布于 2014-07-31 13:26:26

试一下这个解决方案,它对我很有效。

代码语言:javascript
复制
## 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 text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/x-js text/js 
</IfModule>

## EXPIRES CACHING ##
票数 12
EN

Stack Overflow用户

发布于 2012-12-20 20:53:40

您可以将它们添加到htaccess文件或vhost配置中。

查看此处:http://httpd.apache.org/docs/2.2/mod/mod_expires.html

但是除非你拥有这些域名..他们是你的掌控者。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13972629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档