How to force Apache to use manually pre-compressed gz file of CSS and JS files?
Rather than implementing content negotiation yourself using mod_rewrite
(and missing out on more advanced features like q-values, status 406, TCN, etc.) you may want to use mod_negotiation
as discussed in this question. Copying my answer from there:
Options +MultiViews
RemoveType .gz
AddEncoding gzip .gz
<FilesMatch ".+\.tar\.gz$">
RemoveEncoding .gz
# Note: Can use application/x-gzip for backwards-compatibility
AddType application/gzip .gz
</FilesMatch>
This has the added bonus of working for all .gz
files rather than just .css.gz
and .js.gz
and being easily extended for additional encodings.
It does have one major drawback, since only requests for files which do not exist are negotiated a file named foo.js
would make requests for /foo.js
(but not /foo
) return the uncompressed version. This can be avoided using François Marier's solution of renaming uncompressed files with a double extension, so foo.js
is deployed as foo.js.js
. If this restriction isn't too painful for your deployment process, using mod_negotiation
may be worth considering.
Some RewriteRule should handle that quite well.
In a Drupal configuration file I found:
# AddEncoding allows you to have certain browsers uncompress information on the fly.
AddEncoding gzip .gz
#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]
# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]
This will make the job. You can put that either in a <Directory foo/>
section or in a .htaccess file if you do not have access to apache configuration or if you want to slowdown your webserver.
Continuing on regilero answer you should also add a few more lines in order to make sure the server answers with the corresponding Content-Encoding header:
#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]
# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1,E=is_gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1,E=is_gzip:1]
Header set Content-Encoding "gzip" env=is_gzip