Thursday, May 19, 2011

Optimizing Web Page Load Time #gzip

This is going to be a brief tutorial on how to reduce the time it takes your webpage to load though an end users browser.

Firstly try to decrease image sizes by scaling them to 72x72ppi. That is the standard quality for webpages. Also try to use jpg's and compress them as much as possible as long as there is no noticeable distortion in the image. This will save the end user loads of time.

Secondly, compress your javascript and css files so that there are no unnecessary spaces. This will get you a little more space. Search Google for one of several online tools.

Lastly and most importantly use gzip compression. This is done easy enough within your php files by adding the following code to the top of every page.

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

However to compress your css and js files you will first need to compress them locally to the .gz format using a compression tool like 7zip and then upload them to your web server. After that is done add the following code to your .htaccess file.


AddType "text/javascript" .gz
AddEncoding gzip .gz


AddType "text/css" .gz
AddEncoding gzip .gz


RewriteEngine on
ReWriteCond %{HTTP:accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari
ReWriteCond %{REQUEST_FILENAME} !^.+\.gz$
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+) $1.gz [QSA,L]

This will tell the server that whenever a .css or .js file is requested to then look for a .css.gz or .js.gz file and then pass that to the browser. Also Safari is excluded because versions prior to Safari 5 have problem accepting .gz files.

If you follow these procedures you can expect to have much better load times. My main page went from a 26.03 seconds load time to 7.62 seconds load time on 56k. That's a significant improvement.

Remember that if you need assistance on optimizing your website, need a consolation or website created we have the experience and know how to get it done.

Thanks,
Chris Dickerson
Owner / Lead Technician
Open Source Technologies
http://www.OSTechGA.com
(678) 372-9470

No comments:

Post a Comment