Adding Compression to .htaccess File for Apache-hosted Websites

Posted by on Sep 19, 2014 in Web Design, Web Programming

After running my site through Google’s nifty webpage speed analyzer, I noticed some of my resources still weren’t being compressed (like my custom font .eot/.otf files).

I decided to complete the compression of all necessary files once and for all. Upon searching the web and trying different solutions I finally found what appeared to be the best implementation given by one of the answers on stackoverflow here: http://stackoverflow.com/questions/14005758/gzip-compression-htaccess-deflate

Here’s the code I ended up using:

.htaccess Compression Code


# -----------------------------------------------------------------------
# Defining MIME types to ensure the web server actually knows about them.
# -----------------------------------------------------------------------
<IfModule mod_mime.c>
    AddType application/javascript          js
    AddType application/vnd.ms-fontobject   eot
    AddType application/x-font-ttf          ttf ttc
    AddType font/opentype                   otf
    AddType application/x-font-woff         woff
    AddType image/svg+xml                   svg svgz 
    AddEncoding gzip                        svgz
</Ifmodule>

# -----------------------------------------------------------------------
# Compressing output.
# -----------------------------------------------------------------------
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    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/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</Ifmodule>

My site continued to work as it did, and running through Google’s speed analyzer again had me 100% passing compression!

Post a Reply