htaccess Enable Caching with mod_headers - Apache 2.4

Apache 2.4 - Browser caching, optimizations and security

The following are performance optimizations and security enhancements for your website.

<IfModule mod_headers.c>
    Header set Referrer-Policy "same-origin"
    # Disable Last-Modified for performance
    Header unset Last-Modified
 # Disable ETags
 Header unset ETag
 FileEtag None
 # For Security
 Header set X-Frame-Options "SAMEORIGIN"
</IfModule>

Referrer-Policy same-origin - w3.org
Apache 2.4 Module mod_headers


The following setups compression for content, if Gzip compression is enabled on the server.

<IfModule deflate_module>
 AddOutputFilterByType DEFLATE text/text text/html text/css text/plain text/html text/xml application/x-javascript application/javascript text/javascript application/json
</IfModule>

Apache 2.4 Module mod_deflate


Note! Be careful with caching!
Be careful when you enabling browser caching if you set the parameters too long on certain files, users might not be getting the fresh version of your website after updates.

Browser caching with mod_expires

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 12 hours"
# Set expires tags on various file types... so that the browser won't attempt to reload them.
# Images
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/ico "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"

# Video
ExpiresByType video/x-flv "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/mpeg "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access plus 1 year"

# CSS, JavaScript
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/css "access plus 1 month"

# Others
ExpiresByType application/pdf "access plus 1 year"
ExpiresByType text/html "access plus 10 days"
</IfModule>

Apache Module mod_expires