How to enable browser caching and gzip on your browser

How to enable browser caching and gzip on your browser


Enable browser caching ( This can be done by adding a few lines of code to your .htaccess file)


This will make files such as images, js, icons, css, js to only load from your server once, subsequent visits to your site will load those files directly from the cached memory of the visitors device, therefore making load time almost instant.


To enable browser caching for specific duration just add the following lines of code in your htaccess file

 480 weeks
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

# 2 DAYS
<FilesMatch ".(xml|txt)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>

# 2 HOURS
<FilesMatch ".(html|htm)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>



Enable gzip
gzip is a compression system that is available  on almost all browsers. Enabling it can reduce your page’s total size by over 70%, therefore improving load time.
You can enable gzip by adding the code to your .htaccess file.

If your hosting provider has mod_gzip module enabled, the best way to compress your content is to add the following lines to your .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>



Otherwise just add the following line of code instead.


php_value output_handler ob_gzhandler

Note: both the caching & gzip codes for htaccess can be found online as open source to be copied and paste in your htaccess file.

Worths-Alive

1 comment so far

Tom

A very good and informative article indeed.It helps me a lot to enhance my knowledge.I really like the way the writer presented his views.I hope to see more informative and useful articles in future. disposable masks DoubleTrusty MC4 connector can am x3 parts

Leave a Reply