gzip without mod_gzip

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
Arawn
Forum Commoner
Posts: 42
Joined: Sat May 05, 2007 6:03 am

gzip without mod_gzip

Post by Arawn »

I usually don't have to concern myself with gzipping content and the idiosyncrasies of various browsers with regard to gzip content because it's usually not an option on shared servers. However, I started thinking there must be a way to serve gzip content even on a shared server without using a module. It's just content!? After some Googling I've come up with a couples articles for doing just that:

Compress JavaScript and CSS without touching your application code
How to get Apache to send compressed versions of static HTML files
Compressing moo.tools (javascript) even further
Compressed JavaScript (For the User Comments not the article)

Since I already have a build process in place it should be easy to generate the gzip files for HTML, JS and CSS. What to serve, to which browser is the problem.

Code: Select all

<filesmatch "\.js.gz$">
ForceType text/javascript
Header set Content-Encoding: gzip
</filesmatch>
<filesmatch "\.js$">
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !".*Safari.*"
RewriteCond %{HTTP_USER_AGENT} !".*MSIE 4.*"
RewriteCond %{HTTP_USER_AGENT} !".*MSIE 5.*"
RewriteCond %{HTTP_USER_AGENT} !".*MSIE 6.*"
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*).js$ $1.js.gz [L]
ForceType text/javascript
</filesmatch>

<filesmatch "\.html.gz$">
ForceType text/html
Header set Content-Encoding: gzip
</filesmatch>
<filesmatch "\.html$">
RewriteEngine On
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*).html$ $1.html.gz [L]
ForceType text/html
</filesmatch>

<filesmatch "\.css.gz$">
ForceType text/css
Header set Content-Encoding: gzip
</filesmatch>
<filesmatch "\.css$">
RewriteEngine On
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*).css$ $1.css.gz [L]
ForceType text/css
</filesmatch>

Are there any other RewriteCond I should include? I can't think of any problem with gzipping HTML and CSS, are there any problems? I'm think I need a Vary header too?

Anybody else using this kind of setup?
Arawn
Forum Commoner
Posts: 42
Joined: Sat May 05, 2007 6:03 am

Post by Arawn »

This is what I got working on my development server (a finicky Apache2). After I modify the build process I'll try it on one of my own web sites.

Code: Select all

<FilesMatch "\.js$">
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !Safari
RewriteCond %{HTTP_USER_AGENT} !MSIE.4
RewriteCond %{HTTP_USER_AGENT} !MSIE.5
RewriteCond %{HTTP_USER_AGENT} !MSIE.6
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
ForceType text/javascript
</FilesMatch>

<FilesMatch "\.js\.gz$">
ForceType text/javascript
Header set Content-Encoding: gzip
</FilesMatch>

<FilesMatch "\.html$">
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
ForceType text/html
</FilesMatch>

<FilesMatch "\.html\.gz$">
ForceType text/html
Header set Content-Encoding: gzip
</FilesMatch>

<FilesMatch "\.css$">
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
ForceType text/css
</FilesMatch>

<FilesMatch "\.css\.gz$">
ForceType text/css
Header set Content-Encoding: gzip
</filesmatch>
Arawn
Forum Commoner
Posts: 42
Joined: Sat May 05, 2007 6:03 am

Post by Arawn »

Used Browsershot.org to test this setup last night and it was a complete success as far as not effecting the look of the site. IE6, IE7 and Safari looked exactly the same as Firefox. Caching? Well? That I'll have look at over time. Here's the final .htaccess file:

Code: Select all

<FilesMatch "\.js$">
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !Safari
RewriteCond %{HTTP_USER_AGENT} !MSIE.4
RewriteCond %{HTTP_USER_AGENT} !MSIE.5
RewriteCond %{HTTP_USER_AGENT} !MSIE.6
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
ForceType text/javascript
</FilesMatch>

<FilesMatch "\.html$">
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
ForceType text/html
</FilesMatch>

<FilesMatch "\.css$">
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
ForceType text/css
</FilesMatch>

My host does not have mod_headers enabled nor would they enable it. The ForceType does not seem to work so I'll remove that which will let me combine the HTML and CSS FileMatchs. HTML and CSS don't need ForceType as they are sent with the correct Content-Type. Javascript however is being sent with Content-Type application/x-javascript as oppose to text/javascript which I'm not sure make a difference ... time will tell.
Post Reply