2014-02-13 92 views
1

我試圖在我的服務器上壓縮SVG文件,但無論代碼放在.htaccess中,它都不起作用!gzip和deflate壓縮都不適用於使用mod_deflate或AddOutputFilterByType的Apache服務器

這是我的.htaccess代碼:

# ---------------------------------------------------------------------- 
# Proper MIME type for all files 
# ---------------------------------------------------------------------- 

# JavaScript 
# Normalize to standard type (it's sniffed in IE anyways) 
# tools.ietf.org/html/rfc4329#section-7.2 
AddType application/javascript   js jsonp 
AddType application/json    json 

# Audio 
AddType audio/ogg      oga ogg 
AddType audio/mp4      m4a f4a f4b 

# Video 
AddType video/ogg      ogv 
AddType video/mp4      mp4 m4v f4v f4p 
AddType video/webm      webm 
AddType video/x-flv     flv 

# SVG 
# Required for svg webfonts on iPad 
# twitter.com/FontSquirrel/status/14855840545 
AddType  image/svg+xml    svg svgz 
AddEncoding gzip      svgz 

# Webfonts 
AddType application/vnd.ms-fontobject eot 
AddType application/x-font-ttf   ttf ttc 
AddType font/opentype     otf 
AddType application/x-font-woff  woff 

# Assorted types 
AddType image/x-icon      ico 
AddType image/webp       webp 
AddType text/cache-manifest     appcache manifest 
AddType text/x-component     htc 
AddType application/xml      rss atom xml rdf 
AddType application/x-chrome-extension  crx 
AddType application/x-opera-extension  oex 
AddType application/x-xpinstall    xpi 
AddType application/octet-stream   safariextz 
AddType application/x-web-app-manifest+json webapp 
AddType text/x-vcard      vcf 
AddType application/x-shockwave-flash  swf 
AddType text/vtt       vtt 


# ---------------------------------------------------------------------- 
# Gzip compression 
# ---------------------------------------------------------------------- 

<Location /> 
    # Insert filter 
    SetOutputFilter DEFLATE 

    # Netscape 4.x has some problems... 
    BrowserMatch ^Mozilla/4   gzip-only-text/html 

    # Netscape 4.06-4.08 have some more problems 
    BrowserMatch ^Mozilla/4\.0[678] no-gzip 

    # MSIE masquerades as Netscape, but it is fine 
    BrowserMatch \bMSIE    !no-gzip !gzip-only-text/html 
    # Don't compress images 
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary 

    # Make sure proxies don't deliver the wrong content 
    Header append Vary User-Agent env=!dont-vary 
</Location> 


# ---------------------------------------------------------------------- 
# Gzip compression 
# ---------------------------------------------------------------------- 

<IfModule mod_deflate.c> 

    # Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/ 
    <IfModule mod_setenvif.c> 
    <IfModule mod_headers.c> 
     SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding 
     RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding 
    </IfModule> 
    </IfModule> 

    # Compress all output labeled with one of the following MIME-types 
    <IfModule mod_filter.c> 
    AddOutputFilterByType DEFLATE application/atom+xml \ 
            application/javascript \ 
            application/json \ 
            application/rss+xml \ 
            application/xhtml+xml \ 
            application/xml \ 
            application/vnd.ms-fontobject \ 
            application/x-font-ttf \ 
            font/opentype \ 
            image/x-icon \ 
            image/svg+xml \ 
            text/css \ 
            text/html \ 
            text/plain \ 
            text/x-component \ 
            text/xml 
    </IfModule> 

</IfModule> 

當我看着我的SVG文件的響應報頭,似乎沒有gzip壓縮。 當我通過Google PageSpeed Insights分析頁面時,情況也是如此。

任何人都可以幫助我嗎?

+0

gzip是否適用於任何其他類型的文件類型? –

+0

是的,它的確如此。我還注意到附加「Vary:Accept-Encoding」(僅適用於svg文件)也失敗了。我想知道爲什麼... – OpaleGR

+1

我已經做了進一步的測試,並且發現每個圖像/任何文件都沒有被壓縮。我還沒有弄清楚什麼是錯的。最後但並非最不重要的我的託管服務提供商告訴我沒有用... – OpaleGR

回答

0

使用deflate模塊,因爲gzip模塊不是標準Apache安裝的一部分。

添加以下使用mod_deflate模塊:

<filesMatch "\.(svg|gif|jpg|jpeg|png|ico|swf|js|css|html)$"> 
Header set Content-Encoding compress 
# Header set Content-Encoding x-compress 
</filesMatch> 

mod_gzip是一個獨立的項目。

當使用編碼進行響應時,Apache將使用客戶端請求的任何形式(即x-foo或foo)。如果客戶端沒有明確請求特定的表單,Apache將使用AddEncoding指令給出的表單。爲了簡短起見,你應該總是使用x-gzip和x-compress來實現這兩種特定的編碼。應該指定更多最近的編碼,例如deflate,而不使用x-。

參考

+0

我剛剛試過你的代碼,但它不起作用。我會試着給你提供更多的信息:現在我正在使用Apache + cPanel和「Optimize Website」工具,該工具應該可以成功壓縮我的gzip文件,但是...它不起作用(僅適用於image/svg + xml和圖像/ x-圖標)。我認爲這是服務器的錯誤。 – OpaleGR

+0

現在註釋掉''語句。將AddEncoding deflate .svgz或AddEncoding x-gzip .svgz添加到文件的開頭。 –

+0

我已經更新了'compress'和'x-compress'而不是'x-deflate'的答案,如果所有其他都失敗了,它應該可以工作。 –

相關問題