【问题标题】:Gzip working for html, but not javascript or css (Apache)Gzip 适用于 html,但不适用于 javascript 或 css (Apache)
【发布时间】:2016-03-09 14:33:26
【问题描述】:

在我的网站上,html 文件被压缩。

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

服务器使用 Apache/2.2.22。

我正在使用来自 HTML5boilerplate 的 .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。

果然,当我删除 &lt;IfModule mod_filter.c&gt;&lt;IfModule mod_filter.c&gt; 后,一切正常(即,该列表中包含的 javascript、css 和其他文件类型全部压缩包。

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

【问题讨论】:

  • 检查配置的其余部分是否有“gzip-only-text/html”?还是其他的SetOutputFilter?或者这个 .htaccess 根本没有被读取,而 defalte 是在其他地方设置的?

标签: javascript apache .htaccess gzip


【解决方案1】:

您好像错过了什么:

AddType image/svg+xml .svg

AddOutputFilterByType DEFLATE image/svg+xml

https://stackoverflow.com/a/21533084/1491007

最好, 拉尔夫

【讨论】:

    【解决方案2】:

    所以,这很愚蠢。

    正如我所提到的,我使用的是来自 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。

    果然,当我删除 &lt;IfModule mod_filter.c&gt;&lt;IfModule mod_filter.c&gt; 后,一切正常(即,该列表中包含的 javascript、css 和其他文件类型全部压缩包。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2019-02-19
      • 2020-08-12
      • 1970-01-01
      相关资源
      最近更新 更多