1

I'm not 100% sure this is a Craft specific problem, but I'll try here, as it relates to a Craft installation.

After running a number of tests, it seems that my CSS and JavaScript files are not getting GZIP-ed. I have a .htaccess file in my public folder, and no .htaccess files in the subfolders below. My CSS and JS files are stored in /assets/cssand /assets/js respectively (relative to the publicfolder).

My .htaccess file looks like this. Is there something I'm doing wrong? I thought the settings of the .htacces should work for subfolders when not overridden?

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteOptions InheritDown

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
</IfModule>
2
  • Might be a silly question, but is mod_deflate installed/enabled? You also might have better luck with this one on ServerFault or Stackoverflow.
    – Brad Bell
    Aug 15, 2017 at 17:46
  • I was thinking the same, but it seems to be installed, and it works for HTML. At the end of the day I think this was the drop to change away from 1&1! :-)
    – KSP
    Aug 15, 2017 at 18:12

2 Answers 2

2

You can use .htaccess config from HTML Boilerplate for better configuration. There are a lot of useful config option there.

I use this config to enable gzip compression in my personal blog, and it's worked perfectly.

<IfModule mod_deflate.c>

    # Force compression for mangled `Accept-Encoding` request headers
    <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 media types.
    <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>

    <IfModule mod_mime.c>
        AddEncoding gzip              svgz
    </IfModule>

</IfModule>
1
  • Thanks! I actually tried using the boilerplate .htaccess, and it still didn't work for some reason … as I'm tiring of 1&1 I'm trying to set up a version now with Digitalocean + Serverpilot, and see if that changes things.
    – KSP
    Aug 16, 2017 at 23:14
2

Apache supports gzip with two compression modules: mod_gzip and mod_deflate. The latter is supposedly more widely available but some hosts may have one or the other.

mod_gzip is the older of the modules and came with Apache 1.3, mod_deflate is newer and compresses better, though when you're dealing with text like js/css/html, the difference is only a few bytes.

If mod_deflate is giving you fits, you could also try mod_gzip instead.

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  mod_gzip_item_exclude mime ^image/.*  
  mod_gzip_item_include handler ^cgi-script$
</ifModule>

If Apache is running PHP as mod_php, you can also see what modules are running using something like:

<?php
    print_r( apache_get_modules() );
?>
1
  • Thanks! I will try check this. But as per the documentation, mod_deflate should be installed (and mod_gzip not), and gzip DOES work for my HTML files.
    – KSP
    Aug 16, 2017 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.