Get Apache to unzip the file automatically, but only when needed
I would like all the static files of my web server to be compressed locally and whether they are compressed or not, depending on the request.
Answers to How can I precompile files with mod_deflate in Apache 2.x? are close, since indeed, by enabling MultiViews and using the correct AddEncoding, I can get Apache to return the compressed file foo.tar.gz
to me from my webserver when I request foo.tar
, and it comes with the appropriate header Content-Encoding:
.
But this only works if the client includes Accept-Encoding: gzip
in the headers it sends to the server. OTOH, if the client doesn't support gzip encoding, my server just tells me that there is no "acceptable" for me foo.tar
.
I can get Apache to unpack this archive before sending if I use AddOutputFilter INFLATE tar
. But if I do that, then the server also decompresses the content when I request foo.tar.gz
(or when I indicate that I accept gzip encoding), which I clearly don't want.
So how can I get Apache to unpack files when the client does not support encoding the gzip content, but to serve the pre-compressed file in other cases?
EDIT: Based on @ covener's suggestion, I tried the following:
AddEncoding x-gzip .gz .tgz
RemoveType application/x-gzip .gz .tgz
AddType application/x-tar .tgz
<Location /packages>
FilterDeclare smgunzip CONTENT_SET
FilterProvider smgunzip INFLATE req=Accept-Encoding !$gzip
FilterChain smgunzip
</Location>
[Using Apache-2.2.22 here. ] But the result is actually worse than the first three lines: when I ask for a .tar.gz file, it now returns without "Content-Encoding:", and when I ask for a .tar file, I get the contents of tar.gz (then there is still compressed) regardless of the "Accept-Encoding:" header and still without "Content-Encoding:".
source to share
(make sure you have AddEncoding gzip or x-gzip gz or it will break)
2.4
<Directory /home/covener/SRC/2.4.x/built/htdocs>
Options +MultiViews
MultiviewsMatch Any
FilterDeclare gzip CONTENT_SET
FilterProvider gzip INFLATE "! req('Accept-Encoding') =~ /gzip/"
FilterChain gzip
</Directory>
2.2
<Directory /home/covener/SRC/2.2.x/built/htdocs>
Options +MultiViews
MultiviewsMatch Any
FilterDeclare gzip CONTENT_SET
FilterProvider gzip INFLATE req=Accept-Encoding !$gzip
FilterChain gzip
</Directory>
source to share