我最近在firebug上用pagespeed插件分析了我的网站。它建议我设置CSS,JS和图像文件的到期时间。
我想知道我该怎么做?
答案 0 :(得分:47)
当我运行PageSpeed Addon时,这是我用来修复完全相同的东西:
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
这会进入您的.htaccess文件。
阅读此页面,了解有关如何为其他文件类型设置缓存和/或更改缓存长度的更多信息:
http://www.askapache.com/htaccess/apache-speed-cache-control.html
答案 1 :(得分:5)
我想为那些搜索它的人添加这个解决方案....
使用.htaccess
https://webmasters.stackexchange.com/a/5275/37765
<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault A2592000
</FilesMatch>
答案 2 :(得分:0)
我所做的是创建一个文件&#34; expires.conf&#34;并将其包含在Apache的站点文件配置中。如果需要,您可以加入.htaccess。 我的到期日期:
<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 application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
您需要在apache中激活expires模块。
答案 3 :(得分:0)
Add web.config file in your solution to remove "expiration not specified"
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
答案 4 :(得分:-1)
更好的一个(在http://www.paulund.co.uk/set-expire-headers-in-htaccess找到,但由于 0 seconds
无效,我已将其更改为 1 seconds
)
# These are pretty far-future expires headers
# They assume you control versioning with cachebusting query params like: <script src="application.js?20100608">
# 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
# 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 1 seconds"
# Your document html
ExpiresByType text/html "access plus 1 seconds"
# Data
ExpiresByType text/xml "access plus 1 seconds"
ExpiresByType application/xml "access plus 1 seconds"
ExpiresByType application/json "access plus 1 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 1 seconds"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>