Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want Apache to send static files gzip'ed over the wire, but also want Apache to not always gzip them over and over again. So I thought if it wouldn't be possible to deliver an .gz file if it exists. This set-up:

File structure:

|--- style.css
\--- style.css.gz

And the following in an .htaccess:

mod_rewrite rule:

RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.+) $1.gz [L]

And this setting:

AddEncoding x-gzip .gz

Actually, this works insofar as the .gz file is sent instead of the .css, if the request goes to /static/style.css. The problem is only, that the file is delivered as "application/x-gzip" and not as "text/css". Using mod_rewrite's T flag doesn't alter this. Neither does an explicit

AddType text/css .css

Has anyone an idea, how I could achieve the desired behaviour? Or is it unnecessary for some reason I didn't reckon?

EDIT: There is an additional difficulty: Sending the original file to clients without gzip support. Has anyone an idea how this could work?

share|improve this question
I'm amazed this isn't way more obvious, as it seems crazy to have a web server that doesn't do this. I think IIS has a system to cache gzipped copies of static files on first access. mod_deflate doesn't mention such a feature. – rjmunro Nov 11 '09 at 12:54
apache's mod_deflate docs have their own example now: – cweiske Aug 10 '15 at 6:41
up vote 5 down vote accepted

A solution for sending the correct version to browsers that don't accept gzip would be something along the lines of:

RewriteCond %{HTTP:Accept-Encoding} !gzip
...your rules here...

Also, there is another way to change the type, namely:

<FilesMatch .*\.css.gz>
    ForceType text/css

<FilesMatch .*\.js.gz>
    ForceType text/javascript


share|improve this answer
Cool, thanks! Are there docs for the %{HTTP:...} thingy? I didn't see any mentioning of it in the RewriteCond section of Apache 2.2. The FilesMatch was another idea I had but I thought it more verbose. Since both rules need to fire up the regexp engine, there should be no real difference. – Boldewyn Oct 27 '09 at 7:34
OK, found the docs. I just skipped the interesting part. – Boldewyn Oct 27 '09 at 8:05
This isn't a huge problem, but reading that config, if someone manually fetched the .css.gz file, they would get the text/css, when they should get the application/x-gzip type. – rjmunro Nov 11 '09 at 12:56
Why the ! before gzip on the Accept-Encoding line? Surely that's the opposite of what you need? – rjmunro Nov 11 '09 at 13:39
I was thinking the same, it makes sense to have the RewriteCond only match when the client does accept gzip encoding. – David Z Apr 11 '10 at 21:56

Ah, it seems I found a solution: The T flag does not work if set on the same rule, but it sure does, if you spend it a rule of its own:

RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.+) $1.gz

RewriteRule \.css\.gz$ - [T=text/css]
RewriteRule \.js\.gz$ - [T=text/javascript]

Still I would like to hear others' solutions and opinions.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.