2010-11-10 89 views
31

我最近使用firebug上的pagespeed插件分析了我的網站。它建議我設置CSS,JS和圖像文件的過期時間。我如何在CSS,JS和圖像上設置過期

我想知道我該怎麼做?

+1

它真的取決於你使用的是什麼平臺:IIS6/7,Apache等 – jordanbtucker 2010-11-10 06:01:20

+0

我使用Apache作爲我的網絡服務器。我通過我的php文件或php文件顯示這些文件 – KoolKabin 2010-11-10 06:04:11

回答

40

這是我使用的一個修復同樣的事情,當我跑的PageSpeed附加組件:

<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

+3

我得到了500內部服務器錯誤。我認爲我的apache header_module沒有啓用。我有其他方法嗎? – KoolKabin 2010-11-10 06:48:20

+4

您是否收到500錯誤,然後接受答案? – 2013-03-01 03:36:24

+0

thanx哥們它也適用於我3年後即使 – 2014-09-09 07:45:04

0

我要做的就是創建一個文件「expires.conf 「並將其包含在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中到期模塊。

-2

更好的一個(在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> 
5

我想補充該解決方案爲那些尋找它.. ..

其還偉大工程......用.htaccess

https://webmasters.stackexchange.com/a/5275/37765

<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$"> 
    ExpiresActive On 
    ExpiresDefault A2592000 
</FilesMatch> 
+0

非常感謝這個解決方案..... – 2015-02-28 12:16:05

+0

它爲我工作。但是,我想知道如果在添加此代碼後網站發生故障會發生什麼? – Rahul 2017-09-20 10:09:03

相關問題