How To Optimize Your Site With GZIP Compression

This is unexpectedly easy to understand, straightforward and probably a couple of other synonyms. If it makes sense to someone who’s never tried running a server, then that’s good news.

Thanks Sam – I’ll try to keep 'em that way! :slight_smile:

[…] KAYNAK: betterexplained […]

[…] KAYNAK: betterexplained […]

[…] devam edecek… Kaynak: betterexplained […]

[…] Here are parts of todays finds, jotted down for later reference: GZIP compression, HTTP Caching, the Pareto principle (plus the wikipedia equivalent), passing elements to event handlers with jQuery, CSS Compress for WordPress, This day in type, Pingdom, We love TXP, Jon Hicks’ google reader theme, lovely inspiring typography works by Craig Ward… […]

Really decent article … simple and concise.

Thanks, glad you liked it. I try to keep things as simple and short as possible where I can.

Hi,
I use IE 6.0.
When i run an application in the http server i seed header going as “accent-encoding:gzip, deflate”

But when i run an application in the HTTPS server, i DONOT see the header going …

Why does it happen… Any way to solve it?
Thanks and Regards
Silvius

Hi.

I am don’t have direct access to a server because I use a hosting company ipowerweb. When I talked to tech support I was told that they have zlib installed but that “mod_deflate” is not enabled. If there a way that I can compress my html code using php or some alternate method? I have already optimized my html files, css file, javascript and images.

Thank you for your time.

[…] There is a great post over at BetterExplained that goes into more depth about how to implement GZIP compression if you are using Apache 2 and it is worth reading. […]

I just read another ‘How To’ that says you can just change your file name to *.php and add the following php code to the top of the page I wanted to compress following the example below:

This web page is now COMPRESSED!

I then tested the compression at:
http://www.gidnetwork.com/tools/gzip-test.php

What are the pros and cons of the different ways of doing this? What’s better?

Quote:
"Apache actually has two compression options:

  • mod_deflate is easier to set up and is standard.
  • mod_gzip seems more powerful: you can pre-compress content."

Ok… so… now the next question… If pages are “pre compressed” that seems to say that the person browsing your site will have even faster response as they will not have to wait for the server to compress a file each time the page is accessed.
That sounds cool… especially if you site is getting a lot of hits. Did I understand that part correctly??

If so … how do I set it up to work that way and save on server cpu resources and make things even faster by using ‘pre compression’.

Thanks for the great article and the feedback :slight_smile:

Rich

I have no idea what happened… but the PHP code i pasted above doesn’t show up in my post. Here’s a link to it. http://www.desilva.biz/php/zlib.html

Hope this helps… and if someone can give me some feedback regarding my two questions above i would really appreciate it.

Rich

Hi Rich, thanks for writing. I’ll take a crack at your questions:

  1. What’s the difference between using PHP vs Apache compression?

PHP is more convenient if you can’t change server settings. I think apache is better if you have the option because you only need to change 1 place to enable/disable compression, and applies site-wide.

  1. How do you pre-compress content?

I haven’t had to do this personally – unless you’re noticing huge problems I’d say don’t worry about it :). But if you do want to enable it, there seems to be a few ways:

a) Enable pre-compression with mod_gzip: http://schroepl.net/projekte/mod_gzip/config.htm#precompressed

b) Use Apache rewrite rules to redirect requests (index.html) to the pre-compressed version (index.html.gz):

http://www.webmasterworld.com/apache/3387947.htm

c) Use PHP to redirect to the pre-compressed version:

http://www.phpbuilder.com/tips/item.php?id=1128

Of the three, I’d try to go with the Apache solution as mod_gzip can keep the pre-compressed files up to date, etc. Hopefully one of these work for you!

Hi,

Excellent article for the novice like me.

I’ve spent all morning trying to implement compression through .htaccess but I get Error 500 from the SetOutputFilter DEFLATE command.

Does this mean that the compression module is unavailable on my server?

Currently I add a gzip command via php but this solution appears to be more efficient and saves me adding php to the css / js files.

If it’s a module missing should there be any problem asking my hosting company to add?

Help appreciated.

Hi Mike, thanks for the comment. Try downloading the example and running it on your server to check if that works (just to make sure there’s no typos, etc.).

Otherwise, I’m afraid it’s probably something you need to have your hosting company add. I’d be surprised if they didn’t have it or didn’t want to turn it on, since it will save them bandwidth :slight_smile:

Good luck.

can you show

compress all text & html:

AddOutputFilterByType DEFLATE text/html text/plain text/xml

Or, compress certain file types by extension:

<Files *.html>
SetOutputFilter DEFLATE

in GZIP format? It’s what Yahoo recommends http://developer.yahoo.com/performance/rules.html#gzip

Hi Kirk, I checked around and it seems like mod_deflate may actually use the “gzip” encoding mechanism! When I checked the HTTP response, it appears that you have “Content-Encoding: gzip” even when using mod_deflate.

I think “mod_deflate” may be an unfortunate name, as it uses the GZIP format as far as I can tell.

Nice tut, I applied it exactly as indicated to do .JS files but when I use pingdom it still shows my JS file as 45kb, am I missing the point or something? Also when I added this:

SetOutputFilter DEFLATE

I got a 500 internal server error. I used the gzip test and it did say my page was compressed from 33k to 6k, pingdom still shows 6k, I am guessing that you cannot use pingdom to gauge huh?

I put in the .htaccess file to only include .js files, just curious then as to why it is compressing the entire page. I also do not notice any compression for the JS files other then urchin. Other files such as jquery-1.2.1.min.js did not seem to compress, I have read that there is trouble compressing already compressed files if they have used packer to be compressed.

Still a bit confused by all this, hope you can help, thanks.

Also can I use both of these in the .htaccess or only one?

# compress all text & html: AddOutputFilterByType DEFLATE text/html text/plain text/xml

Or, compress certain file types by extension:

<Files *.js>
SetOutputFilter DEFLATE