Nowdays it’s more important than ever to have a website loading as fast as possible; if users don’t see the information they need fast enough, they will press the back button and search it somewhere else. Further more, there were many discussions recently that website speed will be one of the most important seo factors for search engines.
One of the most important things to do to optimize your webpages is to enable the compression of webpages using gzip or inflate extensions. The most convenient way of doing it is to configure it in the htaccess file. The problem is that not all the hosting providers have this option enabled. In case they don’t you have 2 options: to change the hosting provider or to use php and manual compression.
This method assumes 2 steps:
- Enable PHP compression for dynamic files.
- Precompress static resources such as .js and .css files or compress them as PHP files.
PHP Compression
To enable the php compression you need to have GZip or Inflate php extensions enabled. Most of the time they are enabled. To find out if they are available create a new php file containing this line: . Save it and open the specific page in the browser.
Then add the following code at the beginning of the php root file(index.php). If there are more than one files that are directly open by the browser then add this code sequence in the beginning of each one:
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’)) ob_start(“ob_gzhandler”); else ob_start();
Static Files Compression
Because the .css and .js files are static files, they can be compressed by php only if they are processed as php files. For example they can be saved as .css.php and compressed using the above mentioned method, like regular php files. This method is easy to implement for scripts you write from scratch, but for existing solutions might be problematic.
Another method would be to pre-compress them and put directly the compressed version on disk. This method will be detailed in a future post.