Forum Moderators: phranque

Message Too Old, No Replies

Pre-zipped not working

         

fside

3:54 pm on Mar 11, 2008 (gmt 0)

10+ Year Member



I tried this in my root directory .htaccess:

RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.js -f
RewriteRule ^(.+) $1.gz [L]

I have a directory with a "main.js", and a "main.gz". I was hoping the Apache server would send out the main.gz, and that the browser would decompress and treat it as, main.js. But the server sends back - main.js. Is there some error in the above?

jdMorgan

4:31 pm on Mar 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the code is checking for the existence of a file named "main.js.js" as-written, and that file will likely never exist. And even if it did, the logic is still wrong: You're wanting to see if the .gz file exists, not the .js file:

RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/$1.gz -f
RewriteRule ^(([^/]+/)*[^.]+)\.js$ /$1.gz [L]

This will handle any .js file in any (sub)directory or in root. Nested parentheses are used to capture the entire URL-path, excluding the filetype, into $1.

Also make sure that the MIME-type is declared properly for .gz files, using something like:


AddType application/x-gzip .gz

if your server doesn't already return that MIME-type for .gz files.

Jim