Compress Your Web Site

You know how to turn compression on for one or more web pages with PHP, but what if you want to enable compression for your entire web site?

With an Apache Web Server, it’s very easy. All you do is identify the types of files you want to serve compressed, insert the list to your server configuration file, and reload your web server.

Typical types of files you would want to serve compressed every time include:

  • HTML
  • CSS
  • Java Script
  • Plain Text
  • XML

You should also consider other “text” file types, like .json files, for example, if you use or serve them extensively on your site.

You do not want your web server to compress files that are already compressed, such as popular media files and archive files. Typical media files like video, audio and image files are usually already compressed.  In other words, if they exist with file extensions like .mp4, .png, .jpg.mp3 etc., then they are already in some form of a compressed state.

Common (compressed) archive files include those that have .zip, .gz, .bz2, .rar, and .7z extensions.

WordPress does not have a setting anywhere where you can enable compression for your web site. The instructions in this page is recommended, if you want your self-hosted WordPress-powered web site to run optimally.

Apache Web Server

If your web server is Apache, and if the mod_deflate module is enabled on it, the following method is the easiest way to get compression enabled for your entire web site. You just insert something like this to the .htaccess file in your web document root folder:

<IfModule deflate_module>
    # Enable compression for the following file types.
    AddOutputFilterByType            \
     DEFLATE                         \
      application/javascript         \
      text/css                       \
      text/html                      \
      text/javascript                \
      text/plain                     \
      text/xml
</IfModule>

About Apache module mod_deflate.

Or, if you want this to only apply to a particular directory (and its sub-directories), and you are able to add/modify httpd directives to a <VirtualHost> container yourself – where the directory is /usr/local/httpd/htdocs:

<Directory /usr/local/httpd/htdocs>
    <IfModule deflate_module>
        # Enable compression for the following file types.
        AddOutputFilterByType            \
         DEFLATE                         \
          application/javascript         \
          text/css                       \
          text/html                      \
          text/javascript                \
          text/plain                     \
          text/xml
    </IfModule>
</Directory>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>