2011-12-15 97 views
8

我將以下內容輸入到我的「.htacces」文件中,以便開始緩存Web內容。根據Google Page Speed和Yslow的說法,頁面仍然沒有被緩存。模塊是否錯誤?還是應用程序沒有正確顯示數據?htaccess緩存不起作用

網站上的Apache 2.0運行的服務器上

的.htaccess(帶緩存模塊部分):

# Expire headers 
<ifModule mod_expires.c> 
  ExpiresActive On 
  ExpiresDefault "access plus 1 seconds" 
  ExpiresByType image/x-icon "access plus 2592000 seconds" 
  ExpiresByType image/jpeg "access plus 2592000 seconds" 
  ExpiresByType image/png "access plus 2592000 seconds" 
  ExpiresByType image/gif "access plus 2592000 seconds" 
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds" 
  ExpiresByType text/css "access plus 604800 seconds" 
  ExpiresByType text/javascript "access plus 216000 seconds" 
  ExpiresByType application/javascript "access plus 216000 seconds" 
  ExpiresByType application/x-javascript "access plus 216000 seconds" 
  ExpiresByType text/html "access plus 600 seconds" 
  ExpiresByType application/xhtml+xml "access plus 600 seconds" 
</ifModule> 
  
# Cache-Control Headers 
<ifModule mod_headers.c> 
#month 
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$"> 
    Header set Cache-Control "max-age=2592000, public" 
  </filesMatch> 
#week 
  <filesMatch "\.(css|js)$"> 
    Header set Cache-Control "max-age=604800, public" 
  </filesMatch> 
#day 
  <filesMatch "\.(x?html?|php)$"> 
    Header set Cache-Control "max-age=43200, private, must-revalidate" 
  </filesMatch> 
</ifModule> 
# END Cache-Control Headers 
  
# Turn ETags Off 
<ifModule mod_headers.c> 
  Header unset ETag 
</ifModule> 
FileETag None 
  
# Remove Last-Modified Header 
<ifModule mod_headers.c> 
  Header unset Last-Modified 
</ifModule> 
+0

檢查發送的Expires報頭。使用Chrome開發者工具或螢火蟲中的網路標籤。可能是因爲htaccess被禁用,或者mod_expires未啓用。 – Gerben 2011-12-15 16:43:38

回答

19

走進httpd.conf並查找mod_expires線,它不應該被註釋掉。查找mod_headers行,並確保它未被註釋掉。

或(不是一個關鍵的應用程序)有一個簡單的和骯髒的測試:刪除<ifModule mod_expires.c></ifModule>假的東西之間,同樣適用於<ifModule mod_headers.c>,如果你的服務器失敗,500互聯網服務器錯誤,那麼你很可能缺少一個或兩個這些模塊並沒有啓用。如果是這樣,那麼進入httpd.conf並啓用你需要的。

您還可以使用類似REDbot這樣的工具來測試您網站的回覆標題。只需選擇一個資源URL(如指向圖像的URL)並將其粘貼到工具中,即可查看哪些標頭會返回並附帶一些建議。請注意,它遵循域的robots.txt規則,並且不會檢查資源是否被禁止。

就像Gerben所說的那樣,使用firefox中的net選項卡,chrome開發工具或一些等效的Web開發工具可以幫助您查看哪些頭部正在發送和接收。您也不需要設置Cache-Control public。如果您還使用ExpiresByType來電,則不需要使用max age

欲瞭解更多信息閱讀這個偉大的教程:http://www.mnot.net/cache_docs/

而且學習的榜樣:結賬它是如何在html5-boilerplatehttps://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

做對於其他流行的服務器配置實例像lighthttpdNode.jsNginx等。參見: https://github.com/h5bp/server-configs

+0

@Anthony鏈接到這篇文章底部的html5樣板文件相當有幫助。例如,我正在寫`jpg`而不是`jpeg`。感謝那! – 2014-07-09 11:18:05