How to enable GZIP compression and test

Last Updated : |  

Apache servers have already integrated the GZIP compression program and you just have to enable it. With the htaccess this is quickly done in a few steps and your GZIP is activated. The advantage is that you get compressed files delivered faster. There are many ways to do that, but here I would like to introduce the htaccess variant.

Enable GZIP with the htaccess

For us, the DEFLATE (algorithm) is important in the first place that is GZIP encoding of two parts (LZ77 and Huffman-coding), here we can use an AddOutputFilterByType for all applications to compress them in the output. And that could look like this. You can enable the GZIP module in the Apache configuration file, but it should already be enabled. Call configuration file in the console (Terminal)

USERNAME$ nano /etc/httpd/conf/httpd.conf
# Deactivated
# LoadModule deflate_module modules/

# Activated
LoadModule deflate_module modules/


     0  # MBT BEGIN GZIP  1  <IFModule mod_deflate.c>  2  # Set DEFLATE filter  3  SetOutputFilter DEFLATE  4  # Compress on output  5  #text  6  AddOutputFilterByType DEFLATE text/html  7  AddOutputFilterByType DEFLATE text/css  8  AddOutputFilterByType DEFLATE text/javascript  9  AddOutputFilterByType DEFLATE text/plain  10  AddOutputFilterByType DEFLATE text/xml  11  AddOutputFilterByType DEFLATE text/json  12  AddOutputFilterByType DEFLATE text/vtt   13  AddOutputFilterByType DEFLATE text/php  14  AddOutputFilterByType DEFLATE text/x-component  15  # application  16  AddOutputFilterByType DEFLATE application/js  17  AddOutputFilterByType DEFLATE application/javascript  18  AddOutputFilterByType DEFLATE application/rss+xml  19  AddOutputFilterByType DEFLATE application/atom+xml  20  AddOutputFilterByType DEFLATE application/  21  AddOutputFilterByType DEFLATE application/xhtml+xml  22  AddOutputFilterByType DEFLATE application/xml  23  AddOutputFilterByType DEFLATE application/json  24  AddOutputFilterByType DEFLATE application/ld+json   25  # image  26  AddOutputFilterByType DEFLATE image/svg+xml  27  AddOutputFilterByType DEFLATE image/icon  28  AddOutputFilterByType DEFLATE image/ico  29  AddOutputFilterByType DEFLATE image/jpeg  30  AddOutputFilterByType DEFLATE image/jpg  31  AddOutputFilterByType DEFLATE image/gif  32  AddOutputFilterByType DEFLATE image/png  33  # font  34  AddOutputFilterByType DEFLATE font/opentype  35  AddOutputFilterByType DEFLATE font/otf  36  AddOutputFilterByType DEFLATE font/ttf  37  # Remove browser bugs (only needed for really old browsers)  38  BrowserMatch ^Mozilla/4 gzip-only-text/html  39  BrowserMatch ^Mozilla/4\.0[678] no-gzip  40  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html  41      # Make sure proxies don't deliver the wrong content  42      <IfModule mod_headers.c>  43          Header append Vary User-Agent  44      </IfModule>  45  </IFModule>  46  # MBT END GZIP 

    You can also place these lines at the end of your configuration file or in the htaccess. Thus, we have included almost all file types to compress them in the output via GZIP. Below you will also find a tool to check whether the compression is now effective, s.besten you test it first without and then with. I'm 100% sure that it works and you'll get better results with Google Page Speed ;).

    Set and use browser cache

    Now that you have activated the GZIP, we can set the expiration date for our applications and use the browser cache. This brings us another small advantage in terms of speed and we are rated better.

    Used Browser Cache

       0  # MBT BEGIN BROWSERCACHE  1  # Pictures and other resources can be generously cached HTML rather not  2  <IFModule mod_expires.c>  3  ExpiresActive ON  4  # Perhaps better to whitelist expires rules? Perhaps.  5  ExpiresDefault                                      "access plus 60 seconds"  6  # text   7  ExpiresByType text/html                             "access plus 0 seconds"  8  ExpiresByType text/cache-manifest                   "access plus 0 seconds"  9  ExpiresByType text/css                              "access plus 1 month"  10  ExpiresByType text/javascript                       "access plus 1 month"  11  # application  12  ExpiresByType application/xml                       "access plus 60 seconds"  13  ExpiresByType application/json                      "access plus 60 seconds"  14  ExpiresByType application/rss+xml                   "access plus 60 seconds"  15  ExpiresByType application/atom+xml                  "access plus 60 seconds"  16  ExpiresByType application/         "access plus 1 month"  17  ExpiresByType application/javascript                "access plus 1 month"  18  ExpiresByType application/pdf                       "access plus 1 month"  19  # image  20  ExpiresByType image/gif                             "access plus 1 month"  21  ExpiresByType image/png                             "access plus 1 month"  22  ExpiresByType image/jpg                             "access plus 1 month"  23  ExpiresByType image/jpeg                            "access plus 1 month"  24  ExpiresByType image/icon                            "access plus 1 month"  25  ExpiresByType image/ico                             "access plus 1 month"  26  ExpiresByType image/svg+xml                         "access plus 1 month"  27  # audio  28  ExpiresByType audio/ogg                             "access plus 1 month"  29  # video  30  ExpiresByType video/ogg                             "access plus 1 month"  31  ExpiresByType video/mp4                             "access plus 1 month"  32  ExpiresByType video/webm                            "access plus 1 month"  33  # webfonts  34  ExpiresByType font/truetype                         "access plus 1 month"  35  ExpiresByType font/opentype                         "access plus 1 month"  36  </IFModule>  37  # MBT END BROWSERCACHE 

      Now we have applied the full range of browser cache, I have always driven well with these settings and therefore not really screwed, although I think that you can get even more out. So if you have an idea or even a concrete solution always happy with it.

      If you use WordPress then put both code snippets over the WordPress code snippet.

      Testing GZIP compression

      Have you added everything in your htaccess so you can now also test whether everything really grabs and something is compressed. Here you can see how much it is when it is compressed and how much if it was not compressed and much more.

      Well, if your GZIP is working now, what 99% of these two above solutions should apply to, because I'm pretty sure it will work for anyone who uses it. Then you did it and successfully activated your GZIP.


      Your Comment

      * This fields are required, email don't publish

      This field is optional
      Fill this field link your name to your website.

      Data entered in this contact form will be stored in our system to ensure the output of the comment. Your e-mail address will be saved to determine the number of comments and for registration in the future

      I have read the Privacy policy and Terms. I Confirm the submission of the form and the submission of my data.
      tnado © 2022 | All Rights Reserved
      In cooperation with Hyperly