4

In apache htdocs, i want to only have compressed files (gzip file) and based on request's accepted encoding types, if gzip encoding is enabled, I want apache to serve the compressed file and if gzip encoding is not supported, then I want apache to serve by uncompressing the gzip files.

I don't want to keep uncompressed files and use deflate to compress when accessed as this will be inefficient and as by default all clients support gzip encoding.

By having both the compressed and uncompressed files (example.js and example.jsgz both in htdocs directory), I was able to make this work by using RedirectCond on request's accepted encoding and RedirectRules. But this needs two files to be stored (compressed and normal ones). Below is the configuration i used with redirect rules.

<Directory /var/www/app>
AddEncoding gzip .jsgz .cssgz .htmlgz
AddType text/javascript .jsgz
AddType text/css .cssgz
AddType text/html .htmlgz
SetEnv force-no-vary
Header set Cache-Control "private"

RewriteEngine on
# If client accepts compressed files
RewriteCond %{HTTP:Accept-Encoding} gzip
# and if compressed file exists
RewriteCond %{REQUEST_FILENAME}gz -f
# send .htmlgz instead of .html
RewriteRule ^(.+)\.(html|css|js)$ $1.$2gz [L]
</Directory>

I dont want to do as above as I have to keep both versions of each file.

For example:

Contents of app directory inside htdocs

ls app/
example.jsgz

Server side apache configuration for app directory

In this case with MultiView option, I am able to server example.jsgz when request file is example.js as example.js is not present. And the configuration on my apache side is as below:

<Directory /var/www/htdocs/app>
AddEncoding gzip .jsgz .cssgz .htmlgz
AddType text/javascript .jsgz
AddType text/css .cssgz
AddType text/html .htmlgz
Options +Multiviews
SetEnv force-no-vary
Header set Cache-Control "private"
</Directory>

Case 1:

Request headers say gzip encoding is supported. And requested url is example.js and not example.jsgz. This is working and example.jsgz file is served with content encoding as gzip and client is able to decompress and use the js file.

Request URL:http://A.B.C.D/app/example.js
Request Method:GET
Request HTTP headers: 
Accept-Encoding:gzip,deflate,sdch

Response Headers:
Content-Encoding:gzip

Case 2:

Request headers say gzip encoding is not supported. And requested url is example.js and not example.jsgz. This is not working as apache is serving example.jsgz and client is failing as gzip encoding is not supported.

Request URL:http://A.B.C.D/app/example.js
Request Method:GET
Request HTTP headers: 

Response Headers:
Content-Encoding:gzip

Is there a way to handle case 2 when client doesn't support gzip encoding by just having compressed file in htdocs ?

I have read about inflate and deflate options. Also about Multiviews option. But I didn't find example at a directory level when the directory contains multiple types of content (javascript, css, html) in compressed format (gzip encoded).

Thanks in advance

kowsik
  • 61
  • 1
  • 3

1 Answers1

0

Looks like that's what you need http://www.innerjoin.org/apache-compression/howto.html

Though I haven't tried it in any way.

Another option might be to use some script language to run decompression and respond (php, ruby, perl...)

As for performance, having both versions is the best option as it doesn't require any extra efforts.

faron
  • 1,051
  • 7
  • 9