samaxes

Icon

Samuel Santos’ ramblings on web development, open source, java technologies, and web standards. Not necessary in that order.

More on compressing and caching your site 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:

?View Code APACHE
<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:

?View Code APACHE
# 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/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!

Bookmark and Share

Related Posts

9 Responses

  1. [...] 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 [...]

  2. AskApache says:

    Excellent! Very nice FilesMatch regex, great cache-time rules, nice IfModule usage…. this is just great Samuel!

  3. Thanks for your support!

  4. methode says:

    This is an excellent tip for your users, honest.
    It can speed up a Wordpress installation by 50% without any caching plugin.

    Excellent post

  5. Hamachi says:

    Using your 5-row mod_deflate snippet to pack my files, but unfortunately php files aren’t packed.

    What could the problem be?

  6. Does your Apache server have mod_deflate enabled?
    Are your PHP files using .php extension?

  7. Hamachi says:

    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.

  8. I can’t see any reason for it to not work.
    Try asking you hosting provider for support.

  9. [...] 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 [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Sponsors