Easy .htaccess GZIP compression

Posted at

unlike my github project here,
you may as well use your Apache's (or nginx) htaccess rules to apply something called output filter,

...and its quite easy!

Here is how to compress files by mimetype (prefered method):

  1. first, add an empty file named .htaccess, the placement which you'll need to add it varies, the rule-of-thumb is "it applies recursively downwards all folders", so, for the moment, let us create a designated folder named "my_gzip_content" under you web-site tree, down at your host-provider (just FTP for that..), following with creating an empty file (you can do it in the server or just copy an empty text file from your PC using FTP and renaming it .htaccess.

  2. edit, the file adding this content to it:

    <IfModule mod_deflate.c>
    # Force deflate for mangled 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

    # Legacy versions of Apache
    <IfModule !mod_filter.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json text/javascript
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype

  3. - done!
    - ...whaaa?

  4. - yessss!
    all you have to do is place .htm, .html, .txt, .text, .css, .json, .js, .xml, .xhtml, .vnd, .ttf under that folder, and once you'll browse it, your server will serve it along with the "gzip header", and of course- also compress it using common deflate gzip compression methods,
    your browser will get a compressed content (binary) along with several headers that reminds it that it should handle the content as compressed (meaning, decompress before use), and you can save a large amount of traffic, and serve your website a lot faster, which is a plus for Google-crawlers, that will mark your website higher thanks to this..