2012-10-23 93 views
18

我想爲我的網站創建一個htaccess文件,並且pageSpeed見解顯示有圖像和一個css文件沒有到期。htaccess利用瀏覽器緩存的圖像和css

我不確定從哪裏開始或如何做到這一點,我從線上教程的代碼,並想知道這是否足以工作。

<IfModule mod_expires.c> 
ExpiresActive On 
############################################ 
## Add default Expires header 
## http://developer.yahoo.com/performance/rules.html#expires 
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> 
ExpiresDefault "access plus 1 year" 
</FilesMatch> 
</IfModule> 

或者這個代碼做我需要做的事嗎?

感謝

回答

37

嘗試像

<IfModule mod_expires.c> 
    ExpiresActive On 
    ExpiresDefault "access plus 1 seconds" 
    ExpiresByType text/html "access plus 1 seconds" 
    ExpiresByType image/x-icon "access plus 2592000 seconds" 
    ExpiresByType image/gif "access plus 2592000 seconds" 
    ExpiresByType image/jpeg "access plus 2592000 seconds" 
    ExpiresByType image/png "access plus 2592000 seconds" 
    ExpiresByType text/css "access plus 604800 seconds" 
    ExpiresByType text/javascript "access plus 86400 seconds" 
    ExpiresByType application/x-javascript "access plus 86400 seconds" 
</IfModule> 

<FilesMatch "\.(?i:gif|jpe?g|png|ico|css|js|swf)$"> 

    <IfModule mod_headers.c> 
    Header set Cache-Control "max-age=172800, public, must-revalidate" 
    </IfModule> 

</FilesMatch> 
+0

謝謝,我會的標籤之間添加此作爲我嘗試這樣做,它仍然顯示爲過期未設置 – Sam

+1

不添加這個在''你可以添加這個之前或之後這個 –

+0

我已經在上面給出的代碼中添加了'ExpiresActive On到ExpiresByType application/x-javascript「訪問加86400秒''這就是我在.htaccess文件中的所有內容,並且它不工作,請你幫忙。謝謝 – Sam

23

我知道這是一個遲來的答案,但上面並沒有爲我工作。相反,我使用了以下內容:上述

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|html|htm|xml|txt|xsl)$"> 
Header set Cache-Control "max-age=31536050" 
</FilesMatch> 
+1

這對我和谷歌Pagespeed認識到這是一個很好的因素! – gleenk

+0

也爲我工作,thx。最大年齡有多長? – Marko

+0

@Marko這是一年,以秒爲單位。 –

0

關於馬修·約翰遜「的答案,因爲,是它沒有爲我在我的Laravel5.2網站的工作,但我說‘PHP’到列表中,它的工作!謝謝!馬修·約翰遜

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|html|htm|xml|php|txt|xsl)$"> 
Header set Cache-Control "max-age=31536050" 
</FilesMatch> 
6

上面的回答爲我工作,但我也想在.htaccess添加其他文件擴展名。下面的代碼給我一個很好的解決方案。

完全.htaccess代碼通過在谷歌PageSpeed Insight:

  1. 啓用壓縮
  2. 槓桿瀏覽器緩存
# Enable Compression 
<IfModule mod_deflate.c> 
    AddOutputFilterByType DEFLATE application/javascript 
    AddOutputFilterByType DEFLATE application/rss+xml 
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject 
    AddOutputFilterByType DEFLATE application/x-font 
    AddOutputFilterByType DEFLATE application/x-font-opentype 
    AddOutputFilterByType DEFLATE application/x-font-otf 
    AddOutputFilterByType DEFLATE application/x-font-truetype 
    AddOutputFilterByType DEFLATE application/x-font-ttf 
    AddOutputFilterByType DEFLATE application/x-javascript 
    AddOutputFilterByType DEFLATE application/xhtml+xml 
    AddOutputFilterByType DEFLATE application/xml 
    AddOutputFilterByType DEFLATE font/opentype 
    AddOutputFilterByType DEFLATE font/otf 
    AddOutputFilterByType DEFLATE font/ttf 
    AddOutputFilterByType DEFLATE image/svg+xml 
    AddOutputFilterByType DEFLATE image/x-icon 
    AddOutputFilterByType DEFLATE text/css 
    AddOutputFilterByType DEFLATE text/html 
    AddOutputFilterByType DEFLATE text/javascript 
    AddOutputFilterByType DEFLATE text/plain 
</IfModule> 
<IfModule mod_gzip.c> 
    mod_gzip_on Yes 
    mod_gzip_dechunk Yes 
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$ 
    mod_gzip_item_include handler ^cgi-script$ 
    mod_gzip_item_include mime ^text/.* 
    mod_gzip_item_include mime ^application/x-javascript.* 
    mod_gzip_item_exclude mime ^image/.* 
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* 
</IfModule> 

# Leverage Browser 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 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> 
<IfModule mod_headers.c> 
    <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$"> 
    Header set Cache-Control "max-age=2678400, public" 
    </filesmatch> 
    <filesmatch "\.(html|htm)$"> 
    Header set Cache-Control "max-age=7200, private, must-revalidate" 
    </filesmatch> 
    <filesmatch "\.(pdf)$"> 
    Header set Cache-Control "max-age=86400, public" 
    </filesmatch> 
    <filesmatch "\.(js)$"> 
    Header set Cache-Control "max-age=2678400, private" 
    </filesmatch> 
</IfModule> 

也有一些配置的各種web服務器看到here
希望這將有助於獲得100/100的分數。