2011-05-01 202 views
13

我一直在創建一個網站,並通過gtmetrix.com檢查它的速度。我使用了以下.htaccess文件來壓縮.js, .css和更多文件。通過.htaccess Gzip壓縮不起作用

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

ErrorDocument 401 /401.php 
ErrorDocument 403 /403.php 
ErrorDocument 404 /404.php 
ErrorDocument 500 /500.php 

Options Indexes 
IndexOptions FancyIndexing 

ExpiresActive On 
ExpiresDefault "access plus 7 days" 
ExpiresByType application/javascript A2592000 
ExpiresByType application/x-javascript A2592000 
ExpiresByType text/javascript A2592000 
ExpiresByType text/html A2592000 
ExpiresByType text/xml A2592000 
ExpiresByType text/css A2592000 
ExpiresByType text/plain A2592000 
ExpiresByType image/gif A2592000 
ExpiresByType image/jpg A2592000 
ExpiresByType image/jpeg A2592000 
ExpiresByType image/png A2592000 
ExpiresByType image/bmp A2592000 
ExpiresByType application/x-shockwave-flash A2592000 

<FilesMatch "\.(html?|txt)$"> 
    ForceType 'text/html; charset=UTF-8' 
</FilesMatch> 

<FilesMatch "\.(css)$"> 
    ForceType 'text/css; charset=UTF-8' 
</FilesMatch> 

<FilesMatch "\.(js)$"> 
    ForceType 'text/javascript; charset=UTF-8' 
</FilesMatch> 

<FilesMatch "\.(css|js)$"> 
    Header append Vary Accept-Encoding 
</FilesMatch> 

但是當我運行該頁面測試顯示以下-----

Compressing the following resources with gzip could reduce their transfer size by 882B (48% reduction). 

Compressing http://wooflux.co.cc/ could save 645B (48% reduction). 
Compressing http://wooflux.co.cc/style.css could save 237B (51% reduction). 

但是我已經做了gzip壓縮在我.htaccess文件。我不會在這裏發生什麼問題,順便說一下,我對這種編碼風格很陌生。所以請讓我知道如果我做錯了或沒有。

回答

30

我沒有使用mod_gzip的,但這裏是我的.htaccess我通過mod_deflate模塊使用的gzip的.js,.css和其他文件:

#Gzip 
<ifmodule mod_deflate.c> 
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x- javascript application/javascript 
</ifmodule> 
#End Gzip 

如果你是不是已經意識到這一點,Firebug很用於檢查下載的內容是否被壓縮。點擊Firebug中的「Net」選項卡,然後執行shift-f5讓Firefox重新加載所有.js和.css文件,以便它們顯示在Net面板中。單擊.js或.css文件旁邊的「+」,然後單擊「標題」選項卡。如果回覆是gzip,你會在Response Headers部分看到「Content-Encoding gzip」。 IE瀏覽器,Safari瀏覽器和Chrome瀏覽器同樣提供了相同的功能。

one gotcha regarding gzipping .js and .css files on IE6要注意。我認爲它只適用於IE6 SP1用戶。

+2

BTW,確保mod_gzip的和/或MOD_GZIP Apache模塊安裝。你也可能想檢查Apache錯誤日誌是否有任何適用的錯誤。 – 2011-05-01 06:06:28

7

下面是文件擴展名使用的語法。

<IFModule mod_deflate.c>
<filesmatch "\.(js|css|html|jpg|png|php)$">
SetOutputFilter DEFLATE
</filesmatch>
</IFModule>

1

在我的情況下,mod_gzip的或mod_deflate模塊甚至使其啓用並在htaccess文件寫它後不工作。

然後,我結束了在我的PHP文件,它幫我壓縮我的PHP和HTML文件中使用「ob_gzhandler」。這並不像mod_gzip的或mod_deflate模塊一樣好,但暫時爲你服務的情況下,如果你不能在其他得到運氣。

我對在我的上方index.php文件使用,之前裝什麼是代碼,

<? ob_start("ob_gzhandler"); ?>