Gzip 适用于 html,但不适用于 javascript 或 css (Apache)

Gzip working for html, but not javascript or css (Apache)

在我的站点上,html 文件是压缩压缩的。

但是,javascript、css 和 svg 文件不是。

服务器使用Apache/2.2.22.

我正在使用 HTML5 样板中的 .htaccess 文件。这是相关部分:

<IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE "application/atom+xml" \
                                  "application/javascript" \
                                  "application/json" \
                                  "application/ld+json" \
                                  "application/manifest+json" \
                                  "application/rdf+xml" \
                                  "application/rss+xml" \
                                  "application/schema+json" \
                                  "application/vnd.geo+json" \
                                  "application/vnd.ms-fontobject" \
                                  "application/x-font-ttf" \
                                  "application/x-javascript" \
                                  "application/x-web-app-manifest+json" \
                                  "application/xhtml+xml" \
                                  "application/xml" \
                                  "font/eot" \
                                  "font/opentype" \
                                  "image/bmp" \
                                  "image/svg+xml" \
                                  "image/vnd.microsoft.icon" \
                                  "image/x-icon" \
                                  "text/cache-manifest" \
                                  "text/css" \
                                  "text/html" \
                                  "text/javascript" \
                                  "text/plain" \
                                  "text/vcard" \
                                  "text/vnd.rim.location.xloc" \
                                  "text/vtt" \
                                  "text/x-component" \
                                  "text/x-cross-domain-policy" \
                                  "text/xml"

</IfModule>

物有所值 - 它与子文件夹无关。如果我在主目录中放置一个名为 "test.html" 的文件 - 它会被 gzip 压缩。如果我重命名它 "test.js" - 它不是。

[更新]

所以,这很愚蠢。

如前所述,我使用的是 html5boilerplate 中的 .htaccess 文件。

当我仔细查看时,我注意到了这些注释:

# (!) For Apache versions below version 2.3.7 you don't need to
# enable `mod_filter` and can remove the `<IfModule mod_filter.c>`
# and `</IfModule>` lines as `AddOutputFilterByType` is still in
# the core directives.

这适用于我,因为服务器使用 Apache/2.2.22.

果然,当我删除 <IfModule mod_filter.c><IfModule mod_filter.c> 后,一切正常(即 javascript、css 和其他文件该列表中包含的类型 all gzipped.

我会留下这个问题,以防其他人犯同样的错误。

看来你错过了什么:

AddType image/svg+xml .svg

AddOutputFilterByType DEFLATE image/svg+xml

最好的, 拉尔夫

所以,这很愚蠢。

如前所述,我使用的是 html5boilerplate 中的 .htaccess 文件。

当我仔细查看时,我注意到了这些注释:

# (!) For Apache versions below version 2.3.7 you don't need to
# enable `mod_filter` and can remove the `<IfModule mod_filter.c>`
# and `</IfModule>` lines as `AddOutputFilterByType` is still in
# the core directives.

这适用于我,因为服务器使用 Apache/2.2.22.

果然,当我删除 <IfModule mod_filter.c><IfModule mod_filter.c> 后,一切正常(即 javascript、css 和其他文件该列表中包含的类型 all gzipped.

我会留下这个答案,以防其他人犯同样的错误。