【发布时间】:2014-02-13 11:42:17
【问题描述】:
我正在尝试在我的服务器上压缩 SVG 文件,但是我在 .htaccess 中输入的任何代码都不起作用!
这是我的 .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 分析页面时,也会发生同样的情况。
谁能帮帮我?
【问题讨论】:
-
gzip 是否适用于任何其他类型的文件类型?
-
是的,确实如此。我还注意到附加“Vary:Accept-Encoding”(仅适用于 svg 文件)也失败了。我想知道为什么...
-
我做了进一步的测试,我发现每个图像/任何文件都没有压缩。我还没有弄清楚出了什么问题。最后但并非最不重要的一点是,我的托管服务提供商没有告诉我任何有用的信息...
-
@OpaleGR 对 jpg 之类的文件进行 gzip 压缩毫无意义。
-
是的,对 jpg 文件进行 gzip 压缩没有意义,但我确实需要同时 gzip image/svg+xml 和 image/x-icon,因为我的网站在 WebpageTest 和 Google Pagespeed Insights 上的得分是受这种压缩的影响。
标签: apache .htaccess svg compression gzip