2011-04-03 62 views
14

當我測試我的網站SpeedTest我看到很多過期未指定錯誤。您可以在this page看到。指定過期日期爲靜態文件的緩存

我將此代碼添加到我的.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 - 阿帕奇

+0

會發生什麼事,如果你刪除從開頭和結尾的''行?如果模塊沒有安裝,這應該會導致500錯誤。 – clmarquart 2011-04-09 18:49:31

+0

您是否在更改配置文件後重新啓動服務器? – alexy13 2011-04-13 13:23:46

回答

0

也許你沒有加載指定mod_expires,所以整個節是一個空操作。或者,AllowOverride是None並且您的.htaccess文件未處理。

+0

我可以使用Wordpress'.htaccess重寫規則。 – Eray 2011-04-08 12:20:28

+0

ifModule問題呢?你是否使用命令行http客戶端檢查頭文件或只是重新運行速度工具? – covener 2011-04-08 19:41:59

+0

我沒有足夠的經驗。所以我無法檢查ifModule問題。 – Eray 2011-04-09 10:20:47

4

可以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" 
1

我看你有PHP安裝(參考WordPress的),所以我們會盡力診斷你的問題:

  1. 重新啓動你的apache web服務器。如果你擁有Apache服務器(命令行訪問),你可以這樣做like such
  2. create a phpinfo page。這將允許您查看已安裝的apache模塊。檢查您希望使用的mod_expires.c模塊(應該能夠在瀏覽器中爲mod_expires執行CTL-F(查找)請求)。如果你發現它 - 好!如果沒有,你需要在你的apache服務器上安裝mod_expires。如果您擁有您的apache服務器,您可以搜索如何爲特定操作系統安裝模塊。如果沒有,您可以與您的託管服務提供商確認如何安裝此模塊。一旦安裝,繼續。
  3. 修改您的http.conf項,包括你的表達
  4. 重新啓動Apache再次
  5. 測試 「加」=)
35

我喜歡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

+0

您的鏈接已損壞!請更新它。無論如何,+1的答案。 – Zuul 2013-01-19 15:54:46

+1

固定鏈接。謝謝,祖魯。 – Eduardo 2013-01-29 18:25:44

0

你只需要添加:

ExpiresByType application/javascript "access plus 1 year"