.htaccess for Apache 2.4 eTags and 304 cache

This is a small article on how to enable etags and cache to work in a clustered environment.


This article assumes you have Apache 2.4 and its required modules installed ( Headers / Expires / Deflate )

The first step is to add the headers section; this will enable etags for all files and then remove etags for specific files that are usually compressed and added to the -gzip etag bug.

<IfModule mod_headers.c>
FileETag MTime Size
<FilesMatch "\.(js|css|svg|svgz)$">
Header unset ETag
</FilesMatch>
</IfModule>

Mode expire sets far date expires to specific file types. You can change these based on your needs.

<IfModule mod_expires.c>

ExpiresActive On

ExpiresByType image/jpg "access 2 week"

ExpiresByType image/jpeg "access 2 week"

ExpiresByType image/gif "access 2 week"

ExpiresByType image/png "access 2 week"

ExpiresByType image/svg+xml "access 2 week"

ExpiresByType text/css "access 1 month"

ExpiresByType text/html "access 1 week"

ExpiresByType application/pdf "access 1 month"

ExpiresByType text/x-javascript "access 1 month"

ExpiresByType image/x-icon "access 1 year"

ExpiresDefault "access 1 month"

</IfModule>

 With mod deflate you can compress all file types that your website uses.

<IfModule mod_deflate.c>

#DeflateAlterETag NoChange #enable in apache 2.5

AddOutputFilterByType DEFLATE text/plain

AddOutputFilterByType DEFLATE text/html

AddOutputFilterByType DEFLATE text/xml

AddOutputFilterByType DEFLATE text/css

AddOutputFilterByType DEFLATE image/svg+xml

AddOutputFilterByType DEFLATE application/xml

AddOutputFilterByType DEFLATE application/xhtml+xml

AddOutputFilterByType DEFLATE application/rss+xml

AddOutputFilterByType DEFLATE application/javascript

AddOutputFilterByType DEFLATE application/x-javascript

AddOutputFilterByType DEFLATE application/octet-stream #font

</IfModule>

With the above settings, you should be able to serve all your content with compression, etags on a clustered settup.


Written - December 30th 2016 by Jon Wong

  • apache, 304, cache, htaccess
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

404 - File Not Found - Web Error Code

404 Errors are common errors when a user has deleted the source content. In many cases a website...

403 - Forbidden - Web Error Code

The 403 Forbidden error is an HTTP status code that means that accessing the page or resource you...

500 - Internal Server Error - Web Error Code

The 500 Internal Server Error is a very general HTTP status code that means something has gone...

503 - Service Unavailable Error - Web Error Code

The 503 Service Unavailable error is a server-side error, meaning the problem is usually with the...

Blocking Foreign IPs using .htaccess

We have had a few request to block foreign ips from accessing clients websites due to abuse.We...