Home » Htaccess » Speed Tips: Turn On Compression

by 26 comments

mod_deflate disabledApache 2 uses mod_deflate to quickly and easily compress your static .css and .js files before you send them to a client. This speeds up your site like crazy!

.htaccess media caching

FWIW, this is how I configure Apache to set the Expires header:

Header unset ETag
FileETag None
<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"

The reason I remove and disable the ETag is because supposedly some browsers will ignore your Expires header when it's present:

The reason I remove the Last-Modified header is for the same reason:

The reason I set the Cache-Control header to 'public' is so the browser will cache media over HTTPS (see tip #3):

The reason I set the Cache-Control header to 'no-transform' is to prevent proxies from modifying my content.


Turn Gzip Compression On

This goes in your root .htaccess file but if you have access to httpd.conf that is better.

This code uses the FilesMatch directive and the SetOutputFilter DEFLATE directive to only target files ending in .js or .css

<ifModule mod_deflate.c>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE

Without mod_deflate

mod_deflate disabled

With mod_deflate

mod_deflate enabled

Bandwidth Savings

mod_deflate bandwidth savings

Conditional Compression

# Compress everything except images

<location />
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary


Comments Welcome

Information is freedom. Freedom is non-negotiable. So please feel free to modify, copy, republish, sell, or use anything on this site in any way at any time ;)

My Online Tools

Popular Articles
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two.
-- Richard M. Stallman

It's very simple - you read the protocol and write the code. -Bill Joy


Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | |

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain