Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching and gzip compression by htaccess

Can someone provide me with an optimized .htaccess configuration that handles compression, browser caching, proxy caching, etc. for a typical website?

Aside from my visitors, I'm also trying to make Google PageSpeed happy. I wanna use caching and gzip compression through .htaccess please help me with its code!

I want to cache icon,pdf,flv,jpg,png,gif,js,css,swf files for long time

I want to cache xml,txt files for 2 Days

I want to cache html files for 2 Days

And I wanna compress my html,txt,css,js,php files because those have large file size.

Is there any way to gzip images using .htaccess?

like image 561
Hamid Avatar asked Jul 17 '11 14:07

Hamid


1 Answers

# 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>  <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> 
like image 194
Hamid Avatar answered Sep 26 '22 00:09

Hamid