Apache 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!

Table of Contents

  1. Introduction
    1. .htaccess media caching
    2. Turn Gzip Compression On
    3. Without mod_deflate
    4. With mod_deflate
    5. Bandwidth Savings
    6. Conditional Compression

.htaccess media caching

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

Header unset ETag
FileETag None

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.

--Bil

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



SetOutputFilter DEFLATE


Without mod_deflate


With mod_deflate


Bandwidth Savings

Conditional Compression

# Compress everything except images


# 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

Speed Tips: Turn On Compression - AskApache

Charles Torvalds
5 Sep 2007

compression, Htaccess, mod_deflate, mod_gzip, SetOutputFilter

  • Site Map WireShark GNU Non-GNU Tor Project cURL TLDP - Documentation
  • Htaccess Files Hacking Htaccess Javascript Linux Optimization PHP Security Shell Scripting WordPress
  • Base64 Image Converter Raw HTTP Header Debugger Graphical ASCII Text Generator Mac Address Vendor Lookup Who Am I – Your IP Information Request Method Security Scanner .htpasswd file Generator Compress CSS DNS Tracer
Copyright © 2025 AskApache
  • Site Map
  • Htaccess Files
  • Hacking
  • Htaccess
  • Javascript
  • Linux
  • Optimization
  • PHP
  • Security
  • Shell Scripting
  • WordPress
  • Base64 Image Converter
  • Raw HTTP Header Debugger
  • Graphical ASCII Text Generator
  • Mac Address Vendor Lookup
  • Who Am I – Your IP Information
  • Request Method Security Scanner
  • .htpasswd file Generator
  • Compress CSS
  • DNS Tracer
Exit mobile version