简体   繁体   中英

Compressing Javascript & CSS using Gzip

OK, the question may sound familiar - and I've read answers about it everywhere BUT (before you point me to some other answer) none of that has worked for me (htaccess modifications, gzip-ping beforehand, etc) .

So, let me explain you the situation :

  • My website is CodeIgniter-based
  • I'm using XAMPP on Mac (for development purposes), so my actual testing address is something like http://localhost/~drkameleon/mysitename
  • The only .htaccess file I've edited (and that, only for CI-oriented rewritting) resides on the very same directory as my CI installation

So, how do I enable JS/CSS gzip compression? (I don't really care even if the scripts are pre-compressed, they're 100% static anyway...)

I have this through a controller that loads the css and javascript files in two different functions, then run this:

if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
    ob_start("ob_gzhandler");
else ob_start();

// headers

// output

ob_end_flush();

Seems to be doing the trick for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM