Manually gzip CSS and JS files and serve them with Apache

A Compress all the files first. Run the command:

gzip -r *.js *.css

This will find all the Javascript and CSS files in the current directory and its subdirectories. They will be compressed into new files with a .gz extension added (ie, script.js becomes script.js.gz, there will not be a script.js left).

Now edit .htaccess:


RewriteEngine on
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.(js|css)$ $1\.$2\.gz [QSA]

RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1,E=manualgzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1,E=manualgzip:1]

<ifmodule mod_headers.c>
# setup this header only if rewrites above were used
Header set Content-Encoding "gzip" env=manualgzip
</ifmodule>