PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

17 Nov 2013

Compressing resource with gzip using .htaccess

                       After you host your site, check the speed of your site in google insights page speed. You may be get less speed for your site if your information is not compressed. Mostly all sever can compress the files before send it to download. If the server didn't do this, you have to do compress your file. It can be done by .htaccess.

Enable compression using mod_deflate() in .htaccess:

                     Gzip is a server process that compress your files on fly before transmission to the user. It is used to compress text files like html, css and javascript. It reduces the visitor time and your bandwidth. Due to compression, the speed of your site will be increased.
Example:
                <IfModule mod_deflate.c>
                      AddOutputFilterByType DEFLATE text/text
                      AddOutputFilterByType DEFLATE text/plain
                      AddOutputFilterByType DEFLATE text/html
                      AddOutputFilterByType DEFLATE text/css
                      AddOutputFilterByType DEFLATE text/xml
                      AddOutputFilterByType DEFLATE application/x-javascript
                      AddOutputFilterByType DEFLATE application/javascript
                      AddOutputFilterByType DEFLATE text/javascript
                </IfModule>
Explanation:
            Where,
                         mod_deflate.c provides the output filter ' DEFLATE ' which is used to compress the file in your browser before send it to download.

Enable compression using mod_gzip() in .htaccess:

                 
     
                <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>
               

You can also done by your php file. Just add this to your header file.
<?php
           if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')) ob_start('ob_gzhandler');
           else
           ob_start();
?>
After add this content to your .htaccess file, you can check whether your site is compressed or not in  website.

Related Post: