Compression and caching .htaccess

Hi,

I was recently playing around with Google’s PageSpeed Insights and while I don’t agree with all their recommendations it made me look at compression and expiry. I have looked at a few code samples and got this one from Stack Overflow:

# Enable GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</ifmodule>

# Expires Headers - 2678400s = 31 days
<ifmodule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 7200 seconds"
  ExpiresByType image/gif "access plus 2678400 seconds"
  ExpiresByType image/jpeg "access plus 2678400 seconds"
  ExpiresByType image/png "access plus 2678400 seconds"
  ExpiresByType text/css "access plus 518400 seconds"
  ExpiresByType text/javascript "access plus 2678400 seconds"
  ExpiresByType application/x-javascript "access plus 2678400 seconds"
</ifmodule>

# Cache Headers
<ifmodule mod_headers.c>
  # Cache specified files for 31 days
  <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
  Header set Cache-Control "max-age=2678400, public"
  </filesmatch>
  # Cache HTML files for a couple hours
  <filesmatch "\.(html|htm)$">
  Header set Cache-Control "max-age=7200, private, must-revalidate"
  </filesmatch>
  # Cache PDFs for a day
  <filesmatch "\.(pdf)$">
  Header set Cache-Control "max-age=86400, public"
  </filesmatch>
  # Cache Javascripts for 31 days
  <filesmatch "\.(js)$">
  Header set Cache-Control "max-age=2678400, private"
  </filesmatch>
</ifmodule>

I have a few questions:

  1. In 2015, are there any browsers that don’t support Gzip?
  2. If you set expiry headers per above does it still override if you hit refresh?
  3. I get what the expiry headers do, what do cache headers do?
  4. Any issues/improvements to the above code?

Thanks.

No, there has been comprehensive support for gzip for almost a decade now: http://schroepl.net/projekte/mod_gzip/browser.htm

If you refresh, the browser should still attempt to use cached files. Most browsers have some way of doing a ‘hard refresh’ which ignores cached content (shift + F5 in most cases, I believe).

The Cache-Control headers came along after Expires and offer more control. The Cache-Control max-age header is supposed to take precedence over Expires though if both are present: https://tools.ietf.org/html/rfc7234#section-5.3

Thanks, fretburner. I will definitely add gzip then.

On the caching front. I noticed HTML5 Boilerplate doesn’t use Header set Cache-Control. Any ideas why this might be?

Also, when setting expires headers it says:

If you don’t control versioning with filename-based cache busting, you should consider lowering the cache times to something like one week.

Is “filename-based cache busting” setting your resources like this:

<img src="image.jpg?v=2.1.7" alt="" />

And so on for JS, CSS, etc so that when you update the version of your site it automatically refreshes all the resources. Is that the size of it? Is that common practice? If you don’t set expiry headers how long do browsers usually cache for? The compression is a no-brainer but is setting expiry headers worth it? Is it necessary to set expiry and cache headers if they both do the same kind of thing?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.