简体   繁体   中英

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

On my site, the html files are gzipped.

However, the javascript, css, and svg files are not.

The server uses Apache/2.2.22.

I'm using the .htaccess file from HTML5boilerplate. Here's the relevant section:

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

For what it's worth - it's not related to subfolders. If I put a file named "test.html" in the main directory - it's gzipped. If I rename it "test.js" - it's not.

[UPDATE]

So, this was dumb.

As I mentioned, I was using the .htaccess file from html5boilerplate.

When I looked at that closely, I noticed these notes:

# (!) 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.

This applies to me, since the server uses Apache/2.2.22.

Sure enough, as soon as I removed <IfModule mod_filter.c> and <IfModule mod_filter.c> , everything worked as it should have (ie, the javascript, css, and other file types included in that list were all gzipped.

I'll leave this question, just in case anyone else makes the same mistake I did.

It looks like you missed something:

AddType image/svg+xml .svg

AddOutputFilterByType DEFLATE image/svg+xml

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

Best, Ralf

So, this was dumb.

As I mentioned, I was using the .htaccess file from html5boilerplate.

When I looked at that closely, I noticed these notes:

# (!) 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.

This applies to me, since the server uses Apache/2.2.22.

Sure enough, as soon as I removed <IfModule mod_filter.c> and <IfModule mod_filter.c> , everything worked as it should have (ie, the javascript, css, and other file types included in that list were all gzipped.

I'll leave this answer, just in case anyone else makes the same mistake I did.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM