.htaccess gzip and file compression

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
kwansan
Forum Members
Forum Members
Posts: 35
Joined: Mon Apr 17, 2006 7:13 am

.htaccess gzip and file compression

Post by kwansan »

Hi,

I am new here, first I would like to give a standing ovation to cmsms.  It is truly a great cms and it is truly simple.  I have used mumbo oops I mean mambo phpwcms and drupal in the past.  However, cmsms has filled the niche of template flexibility from a designers stand point.  I had cmsms installed and up and running and designing my site in 10 minutes.  ;D ;D

However, I have my first question that I could not find on the forum.  I have the majority of my site running through cmsms and my order form running outside of cmsms.  I am currently optimizing my site for faster performance.  In my order form I have added to the top of the file to compress on load.  Files went from 136,000 bytes to 88,000 bytes and significant improvement.

However, when I added as a user agent php_compress and did a call from my cmsms template as {php_compress} it added 70,000 bytes to the file!?!  ??? ???

Not sure why it did that.  So, I scrapped that and decided to make a .htaccess file to compress the files from cmsms.  I would either get an internal 500 error or it would have no effect on compressing the files.

I am wondering if cmsms is already compressed with zlib and if so I am guessing I would not be able to further compress with .htaccess.  Or if it is not compressed has anyone been able to sucessfully compress with .htaccess.  I am a bit perplexed and any info would be helpful.  Thank you!

Sincerely,
David
kwansan
Forum Members
Forum Members
Posts: 35
Joined: Mon Apr 17, 2006 7:13 am

Re: .htaccess gzip and file compression

Post by kwansan »

Ok I solved my own problem.

I added the line to the top of my cmsms include.php file. It has to be as the first line or it will not work.  I rechecked my load time and here is the result before and after of 2 of my pages.

Index page before not including image file size:
35,950 bytes loads 17.95 seconds 56k modem (not acceptable)

Index page after not including image file size:
5,284 bytes loads 10.56 seconds 56k modem ( ;D ;D ;D ;D)

Hosting plan comparison page before not including image file size:
72,607 bytes loads 23 seconds 56k modem (will put you to sleep)

Hosting plan comparison page  after compression not including image file size:
7,101 bytes loads 8.54 seconds 56k modem ( ;D ;D ;D)

Note the extra time in loading is because of different images used.  I just need to further optimize my images with photoshop.  I use 56k as a benchmark speed because I am on cable modem and spoiled.  Sometimes I get carried away with graphics so I have to keep myself in check.  It is the easiest way to optimize your files.  If it does not work than it maybe the way your hosting server is setup.  Another way would be to use .htaccess.  But, that is not recommended if you are not familiar with .htaccess.

Posted that incase anyone was interested in how to optimize their files.

Happy coding  ;D ;D ;D
Last edited by kwansan on Sun Apr 30, 2006 3:56 am, edited 1 time in total.
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm

Re: .htaccess gzip and file compression

Post by Russ »

Thanks  kwansan for reminding me, I tried this and see around a 75% saving, which seems pretty good to me :)

This mod is currenlty running on our test site (which is not very graphics heavy) but you can try it out by putting the following url:
http://www.cms.shoesforindustry.net/cms ... leryb.html

into a tester like:
http://www.whatsmyip.org/mod_gzip_test/

Does anybody know of any downsides to using this with CMS?

Russ
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm

Re: .htaccess gzip and file compression

Post by Ted »

Because of the many many different web server setups, doing something like this makes me kind of nervous.  Some apache setups do this automatically, and some php setups do too.  There is a way to do this directly in php.ini (and I assume an .htaccess file) without have to modify the code.
Piratos

Re: .htaccess gzip and file compression

Post by Piratos »

ob_gzhandler() requires the zlib extension.
ob_gzhandler() is intended to be used as a callback function for ob_start() to help facilitate sending gz-encoded data to web browsers that support compressed web pages. Before ob_gzhandler() actually sends compressed data, it determines what type of content encoding the browser will accept ("gzip", "deflate" or none at all) and will return its output accordingly. All browsers are supported since it's up to the browser to send the correct header saying that it accepts compressed web pages.

You should put this code in the index.php .
If you have zlib the function gzopen must exists.

//@ob_start();
if (function_exists('gzopen'))@ob_start('ob_gzhandler'); else @ob_start();
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: .htaccess gzip and file compression

Post by Dr.CSS »

has anyone tried commpressing the HTML and CSS with something like PSPad editor, copy/paste ,compress and copy/paste back,
HTML and CSS compressed are a little hard to read but,,

      mark
kwansan
Forum Members
Forum Members
Posts: 35
Joined: Mon Apr 17, 2006 7:13 am

Re: .htaccess gzip and file compression

Post by kwansan »

Hi,

Wow, so much great feedback.  ;D
Ted wrote: Because of the many many different web server setups, doing something like this makes me kind of nervous.  Some apache setups do this automatically, and some php setups do too.  There is a way to do this directly in php.ini (and I assume an .htaccess file) without have to modify the code.
Ted, I totally agree with you and that is why I did not suggest it was included with cmsms.  It may cause error messages.  Simple way to test it is insert the code ob_gzhandler and if you get errors delete that line or restore your backup copy.  Always make sure you make a backup copy of any file you are working with. 

If you have access to php.ini file than the compression can be done from there, but not all host has this option.  If php is ok to use in your .htaccess file than it can be achieved from there as well.  Several ways to do it.  However, for the inexperienced it may be easier to simply add the ob_gzhandler to the top of one file in cmsms.  If it is not the top line or if you added it several times you will get an error as well.

Russ, great link!  I'm not good with math but I think my files were compressed about 85% and today I got my graphics cut to half so several of pages are now within sub 8. 

Piratos and Russ has expanded in great detail what it is and what it does.  Great explanation!
Piratos wrote: You should put this code in the index.php .
If you have zlib the function gzopen must exists.

//@ob_start();
if (function_exists('gzopen'))@ob_start('ob_gzhandler'); else @ob_start();
Piratos, I like the way you expanded on the generic code, so if my server setup changes it seems no harm done.  Excellent, I will give it a go and let you know if there are problems.  Thanks for the tip!
maksbud wrote: has anyone tried commpressing the HTML and CSS with something like PSPad editor, copy/paste ,compress and copy/paste back,
HTML and CSS compressed are a little hard to read but,,
Maksbud, the reason I used the built in ob_gzhandler is to prevent extra work to copy and paste files each and everytime I needed compression.

Thanks! for the input everyone.

Sincerely,
David
kwansan
Forum Members
Forum Members
Posts: 35
Joined: Mon Apr 17, 2006 7:13 am

Re: .htaccess gzip and file compression

Post by kwansan »

Hi,

Just an update for anyone who is interested. 
Piratos wrote:

You should put this code in the index.php .
If you have zlib the function gzopen must exists.

//@ob_start();
if (function_exists('gzopen'))@ob_start('ob_gzhandler'); else @ob_start();
Piratos, I added that line to the cmsms index and it only gave me about 50% compression.  When used in the include file it gives me about 85% compression.

My original uncompressed page was 61,279 bytes.  Compressed page with snippet added to cmsms index.php 32,142 bytes.  Same compressed page with snippet added to cmsms include.php 19,072 bytes. 

I am thinking when added to include.php more of cmsms files are being compressed as well as my site pages.  However, when called from the index.php several of cmsms files are not being added to the compression.  I am new to cmsms so I would have to take a deeper look when I have more time.  =).

If anyone wants to comment feel free to do so.  thanks!

Sincerely,
David
Post Reply

Return to “CMSMS Core”