hey guys, i have quiet a number of .css and .js files on my website <head></head> pages. how do i compress them with gzip?

you can specify filetypes for compression in apache's .htaccess file

<IfModule mod_gzip.c>     
mod_gzip_on       Yes     
mod_gzip_dechunk  Yes     
mod_gzip_item_include file      \.(html?|txt|css|js|php|pl)$   
mod_gzip_item_include handler   ^cgi-script$     
mod_gzip_item_include mime      ^text/.*     
mod_gzip_item_include mime      ^application/x-javascript.*     
mod_gzip_item_exclude mime      ^image/.*     
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* 
</IfModule>

which acts on all files of the designated extension or mime
the text above is from my .htaccess file
or in php on a per file basis
example style.css renamed to style.css.php

<?php header ('content-type:text/css');
ob_start("ob_gzhandler"); ?>
.rss-box {margin:1em 3%;padding:4px 8px;background-color:#ededed;border:2px dashed #7485CA;}
.rss-title, rss-title a {font-family:"American Typewriter", "Trebuchet MS", Trebuchet, Lucida, sans-serif;font-size:100%;font-weight:bold;margin:5px 0;padding:0;letter-spacing:1px;}
//huge amount of css
<?php ob_flush();?>

it may be more productive to combine the .js files and .css files into 1 if they are small,
other ways to speed up your site
http has limits on simultaneous file transfers from a single domain
create subdomains on your site, I have www pics img, as subdomains so can parallel 2 files from each subdomain (=6) instead of only 2 from www, all subdomsins point to the same root of mydomain.com keep paths constant

thanks for the reply almostbob, im still new on this , i just heard of the gzip compression and thought i should ask. im more interested in php on a per file basis. where do i put the script? the same script can be used for Javascript as well?

my windows host does not support .htaccess

the script is two lines at the start of each file
and 1 line at the end
in this case rename files *.js to *.js.php(so php will process the file, and alter calls in the header to .js.php)
and add the first two lines before and the third line after

<?php header ('content-type:text/javascript');
ob_start("ob_gzhandler"); ?>
<?php ob_flush();?>

for css rename *.css to *.css.php(so php will process the file, and alter calls in the header to .css.php)
and add the first two lines before and the third line after

<?php header ('content-type:text/css');
ob_start("ob_gzhandler"); ?>
<?php ob_flush();?>

Its not a huge edit, since your files are php, they all use includes eg include('menu.php'); for all elements that are identical, the header footer menu etc, so you only need to edit one file to change the calls to *.js *.css to *.js.php *.css.php

thanks so much almostbob :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.