Jan 6, 2009
Speed up your site by compressing and caching your content with .htaccess
“.htaccess - gzip and cache your site for faster loading and bandwidth saving” is one of the most popular posts on samaxes.
It’s basically on how to compress and cache your site content with Apache and .htaccess file.
It works like a charm, but it’s not yet the perfect configuration for me.
I wanted something that I can use out-of-the-box without having to rely on external extension modules or tools.
If you are lucky enough to have Apache 2 with your hosting provider you can use the mod_deflate module that comes bundled with it.
In order to compress your text files with this Apache’s module you just have to add the following lines to your .htaccess file:
<IfModule mod_deflate.c> <FilesMatch "\.(css|js|x?html?|php)$"> SetOutputFilter DEFLATE </FilesMatch> </IfModule> |
This will gzip all your *.css, *.js, *.html, *.html, *.xhtml, and *.php files.
A great .htaccess file example that will gzip your text files and cache all your static files, may look like:
# BEGIN Compress text files <IfModule mod_deflate.c> <FilesMatch "\.(css|js|x?html?|php)$"> SetOutputFilter DEFLATE </FilesMatch> </IfModule> # END Compress text files # BEGIN Expire headers <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 1 seconds" ExpiresByType image/x-icon "access plus 2592000 seconds" ExpiresByType image/jpeg "access plus 2592000 seconds" ExpiresByType image/png "access plus 2592000 seconds" ExpiresByType image/gif "access plus 2592000 seconds" ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds" ExpiresByType text/css "access plus 604800 seconds" ExpiresByType text/javascript "access plus 216000 seconds" ExpiresByType application/javascript "access plus 216000 seconds" ExpiresByType application/x-javascript "access plus 216000 seconds" ExpiresByType text/html "access plus 600 seconds" ExpiresByType application/xhtml+xml "access plus 600 seconds" </IfModule> # END Expire headers # BEGIN Cache-Control Headers <IfModule mod_headers.c> <FilesMatch "\.(ico|jpe?g|png|gif|swf)$"> Header set Cache-Control "max-age=2592000, public" </FilesMatch> <FilesMatch "\.(css)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch> <FilesMatch "\.(js)$"> Header set Cache-Control "max-age=216000, private" </FilesMatch> <FilesMatch "\.(x?html?|php)$"> Header set Cache-Control "max-age=600, private, must-revalidate" </FilesMatch> </IfModule> # END Cache-Control Headers # BEGIN Turn ETags Off <IfModule mod_headers.c> Header unset ETag </IfModule> FileETag None # END Turn ETags Off # BEGIN Remove Last-Modified Header <IfModule mod_headers.c> Header unset Last-Modified </IfModule> # END Remove Last-Modified Header |
This will surely improve your site performance by one order of magnitude. Try it!
[...] rest is here: More on compressing and caching your site with .htaccess Related ArticlesBookmarksTags KSK INC New Website The rest of the site is PHP, CSS and [...]
Excellent! Very nice FilesMatch regex, great cache-time rules, nice IfModule usage…. this is just great Samuel!
Thanks for your support!
This is an excellent tip for your users, honest.
It can speed up a Wordpress installation by 50% without any caching plugin.
Excellent post
Using your 5-row mod_deflate snippet to pack my files, but unfortunately php files aren’t packed.
What could the problem be?
Does your Apache server have
mod_deflateenabled?Are your PHP files using
.phpextension?Apache is v2. JS, CSS and HTML files are compressed (only compressing PHP doesn’t work), so I guess it’s enabled.
PHP files have .php extensions.
I can’t see any reason for it to not work.
Try asking you hosting provider for support.
[...] and unzipped automatically by a web browser on the user’s computer. I read this wonderful post and decided to implement Gzip. While WP Super Cache gave me one magnitude of speed, another [...]
Samuel -THANK YOU. I am pursuing an entirely different strategy for speeding services and this seems to fit quite nicely within that paradigm without sacrificing dynamic content.
Thank you for your wonderful article. I implemented this in my site and the downloading speed has increased greatly. YSlow grade has improved from C to B. Some more things need to be done to get a grade of A!
Thank you so much for this information; it has solved my yslow problems making it Grade A.
Highly recommended.
Thanks again.
Wordpress™ Speed optimieren ohne Plugin…
Das Wordpress Blog zu optimieren ist seit Ankündigung des Google™ Speedranking ein Thema, das durch die Blogger Szene schießt. Einige Tipps konnten wir euch schon in dem Artikel „Webmacht Google™ bringt das Page Speed Ranking“ geben…
[...] (via) [...]
[...] Zum anderen habe ich auch am Apachen bzw. MySQL-Server weitere Möglichkeiten der Optimierung ausprobiert. Zur Leistungssteigerung des MySQL-Server empfehlen sich die Tools MySQL-Tuner bzw. tuning-primer. Damit kann man sich langsam an möglichst optimale Einstellungen für den eigenen Einsatzzweck herantasten (man sollte aufjedenfall den möglichen maximalen Speicherverbrauch im Auge behalten). Beim Apachen sind es weniger Tuningeinstellungen für ihn selbst, als für die Auslieferung der Daten. Seit langem schon im Einsatz ist bei mir mod_deflate (davor schon mod_gzip) um Webseiten vor der Auslieferung zu komprimieren. Neu hinzugekommen ist nun mod_expires um die Caching-Funktionen von Proxys bzw. dem Browser besser zu nutzen. Ein paar Einstellungsempfehlungen hierzu findet ihr z.b. hier oder hier. [...]
Are you using this on your site? I’ve added this to my blogs, but ySlow firefox addon doesn’t seem to say that the expire headers are set, these are for both my blogs as well as your site
I’m using a script very similar to this one except for
mod_deflatemodule.Unfortunately my hosting provider is still using Apache 1.3.x; only Apache 2 or greater comes bundled with it.
[...] CachingCombining the two tips above, gzip and caching, the code below is a brilliant snippet from SAMUEL SANTOS site:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 [...]
[...] A good link to .htaccess info: http://www.samaxes.com/2009/01/more-on-compressing-and-caching-your-site-with-htaccess/ [...]
Just wanted to say thanks for this post, I added this to my .htaccess file and knocked off 2+ seconds on my page load times, and bumped up a letter grade in YSlow! I’m putting it on all my sites.
[...] Site compressing and caching with Apache .htaccess April 3, 2010 | Posted by admin http://www.samaxes.com/2009/01/more-on-compressing-and-caching-your-site-with-htaccess/ [...]
thanks for the support .. great help.. my site is good on google score now
Hi Santos
Just wanna drop by to say Hello and Thanks.
I used the second htaccess code above and everything went perfect.
I’m amazed by the instant faster pageload time of my WP site.
Thanks again for this valuable post.
BTW, what is Etag ? and why do we unset it ?
Entity tags (ETags) are a mechanism to check for a newer version of a cached file.
By removing the ETag header, you disable caches and browsers from being able to validate files, so they are forced to rely on your Cache-Control and Expires header.
Hi again Santos
Thanks for the information.
THANK YOU VERY MUCH.
Simply great! Thank you for this as it is perfect!
Well this is great
lets see if it works well or not !!!:P
This is a great resource. I have linked to it in a post I wrote about my meddlings, er optimizations, in WordPress at http://icanhazdot.net/2010/03/23/some-wordpress-stuff/.
I added the following about your code to bypass the compression process for files that already have compressed versions that could be sent with a simple rewrite.
RewriteEngine on
#Check to see if browser can accept gzip files. If so and we have it - serve it!
ReWriteCond %{HTTP:accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari
#make sure there's no trailing .gz on the url
ReWriteCond %{REQUEST_FILENAME} !^.+\.gz$
#check to see if a .gz version of the file exists.
RewriteCond %{REQUEST_FILENAME}.gz -f
#All conditions met so add .gz to URL filename (invisibly)
RewriteRule ^(.+) $1.gz [QSA,L]
Thanks for the post - it helped me out a lot.
Great code snippet Ewen. Thanks for sharing!
Fantastic!! My site is faster, so faster…Thanks…
[...] Speed up your site by compressing and caching your content [...]