2013-02-25 69 views
6

通過Google網站管理員工具的PageSpeed分析器運行我的網頁後,它向我報告我的資源沒有被緩存。以下是我直接從H5BP獲取的.htaccess文件中的代碼。我是否正確地假設下面的過期設置正確,並且在我的實施中出現了問題,或者我是否必須爲每個文件名設置明確的到期期限,而不是對廣義的泛化進行設置?控制緩存過期

# ---------------------------------------------------------------------- 
# 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 

# Perhaps better to whitelist expires rules? Perhaps. 
    ExpiresDefault       "access plus 1 month" 

# cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5) 
    ExpiresByType text/cache-manifest  "access plus 0 seconds" 

# Your document html 
    ExpiresByType text/html     "access plus 0 seconds" 

# Data 
    ExpiresByType application/json   "access plus 0 seconds" 
    ExpiresByType application/xml   "access plus 0 seconds" 
    ExpiresByType text/xml     "access plus 0 seconds" 

# Feed 
    ExpiresByType application/atom+xml  "access plus 1 hour" 
    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 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" 

# HTC files (css3pie) 
    ExpiresByType text/x-component   "access plus 1 month" 

# Webfonts 
    ExpiresByType application/vnd.ms-fontobject "access plus 1 month" 
    ExpiresByType application/x-font-ttf "access plus 1 month" 
    ExpiresByType application/x-font-woff "access plus 1 month" 
    ExpiresByType font/opentype    "access plus 1 month" 
    ExpiresByType image/svg+xml    "access plus 1 month" 

# CSS and JavaScript 
    ExpiresByType application/javascript "access plus 1 year" 
    ExpiresByType text/css     "access plus 1 year" 

</IfModule> 

回答

3

看起來不錯。

要麼你沒有指定mod_expires安裝模塊(1百萬不)或其他一些.htaccesshttpd.conf指令禁用這些其他地方。請使用頁面速度工具抓取您的文件發送的實際標題,然後粘貼。

+0

我很確定必須有另一個htaccess文件覆蓋它,因爲我與某人共享測試服務器。不過,我只是想確保我所擁有的是好的。謝謝! – 2013-02-26 17:10:23

+0

我有完全相同的.htaccess文件(甚至包括評論,例如「Thanks Remy」)。 但是我也收到seo分析的消息,說瀏覽器緩存沒有被利用。 如何檢查是否 a)我已安裝mod_expires並且 b)有重寫? thx – 2016-12-18 13:51:37