Apache: Send pre-packed gzip’ed files

apache-2.2cachegziprewrite

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:

static/
|
|--- 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?

Best Answer

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>

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

HTH.