Html 添加过期标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13972629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Expires headers
提问by sami
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
When testing the site using yahoo YSLOW it says above message. So I don't know how to add expire headers. Any help would be appreciated?
使用 yahoo YSLOW 测试站点时,上面显示了消息。所以我不知道如何添加过期标头。任何帮助,将不胜感激?
回答by pixelistik
The easiest way to add these headers is a .htaccess
file that adds some configuration to your server. If the assets are hosted on a server that you don't control, there's nothing you can do about it.
添加这些标头的最简单方法是.htaccess
向服务器添加一些配置的文件。如果资产托管在您无法控制的服务器上,则您无能为力。
Note that some hosting providers will not let you use .htaccess
files, so check their terms if it doesn't seem to work.
请注意,某些托管服务提供商不允许您使用.htaccess
文件,因此如果它似乎不起作用,请检查他们的条款。
The HTML5Boilerplate project has an excellent .htaccess
file that covers the necessary settings. See the relevant part of the file at their Github repository
HTML5Boilerplate 项目有一个很好的.htaccess
文件,涵盖了必要的设置。在他们的Github 存储库中查看文件的相关部分
These are the important bits
这些是重要的部分
# ----------------------------------------------------------------------
# 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>
They have documented what that file does, the most important bit is that you need to rename your CSS and Javascript files whenever they change, because your visitor's browsers will not check them again for a year, once they are cached.
他们已经记录了该文件的作用,最重要的一点是您需要在更改时重命名 CSS 和 Javascript 文件,因为一旦它们被缓存,您的访问者的浏览器将不会在一年内再次检查它们。
回答by sami
try this solution and it is working fine for me
试试这个解决方案,它对我来说很好用
## 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 ##
回答by Sorin Trimbitas
You can add them in your htaccess file or vhost configuration.
您可以将它们添加到您的 htaccess 文件或 vhost 配置中。
See here : http://httpd.apache.org/docs/2.2/mod/mod_expires.html
见这里:http: //httpd.apache.org/docs/2.2/mod/mod_expires.html
But unless you own those domains .. they are our of your control.
但除非您拥有这些域……它们是我们的控制权。
回答by user8031347
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
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"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
回答by Mohammad Shadman
In ASP.NET there is similar object, you can use Caching Portions in WebFormsUserControls in order to cache objects of a page for a period of time and save server resources. This is also known as fragment caching.
If you include this code to top of your user control, a version of the control stored in the output cache for 150 seconds.
You can create your own control that would contain expire header for a specific resource you want.
在 ASP.NET 中也有类似的对象,您可以使用 WebFormsUserControls 中的缓存部分来缓存页面的对象一段时间并节省服务器资源。这也称为片段缓存。
如果将此代码包含在用户控件的顶部,则该控件的版本将在输出缓存中存储 150 秒。您可以创建自己的控件,其中包含您想要的特定资源的过期标头。
<%@ OutputCache Duration="150" VaryByParam="None" %>
This article explain it completely: Caching Portions of an ASP.NET Page
这篇文章完整地解释了它: 缓存 ASP.NET 页面的部分