Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sabrysuleiman/e63ae001179c4eb472e89ad40b24882a to your computer and use it in GitHub Desktop.
Save sabrysuleiman/e63ae001179c4eb472e89ad40b24882a to your computer and use it in GitHub Desktop.
Serve static assets with an efficient cache policy 
<IfModule mod_expires.c>
ExpiresActive On
# Images (e.g., JPEG, PNG, GIF, SVG)
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year" #favicon
# CSS and JavaScript
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month" #legacy
ExpiresByType text/javascript "access plus 1 month" #legacy
# Fonts
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType application/x-font-opentype "access plus 1 month"
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/font-woff2 "access plus 1 month"
ExpiresByType application/vnd.font-awesome "access plus 1 month"
# HTML (consider shorter cache for dynamic content)
ExpiresByType text/html "access plus 1 hour"
# Other static files (e.g., PDFs)
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/zip "access plus 1 month"
ExpiresByType application/octet-stream "access plus 1 month" #general binary files
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(ico|jpe?g|png|gif|svg|webp|css|js|ttf|ttc|otf|eot|woff2?|pdf|zip)$">
Header set Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(html|htm)$">
Header set Cache-Control "private, must-revalidate"
</FilesMatch>
#prevent hotlinking, optional
<FilesMatch "\.(jpe?g|png|gif|svg|webp)$">
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpe?g|png|gif|svg|webp)$ - [F,L]
</FilesMatch>
</IfModule>
<IfModule mod_deflate.c>
# Compress text files, HTML, CSS, JavaScript, XML and fonts:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/font-woff
AddOutputFilterByType DEFLATE application/font-woff2
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
@sabrysuleiman
Copy link
Author

Key improvements and explanations:

mod_expires for long-term caching:
This module sets Expires headers, telling browsers how long to cache resources. Very long cache times are used for static assets that rarely change (images, fonts, etc.).
access plus 1 year is a good starting point for assets that are versioned (e.g., style.12345.css).
access plus 1 month is used for css, js, and fonts.
access plus 1 hour is used for html, as it is often dynamic.
mod_headers for Cache-Control:
Sets the Cache-Control header, which provides more fine-grained caching control.
public allows caching by browsers and CDNs.
private, must-revalidate is crucial for HTML. private prevents caching by shared caches (like CDNs), and must-revalidate ensures the browser checks with the server for updates after the cache expires.
FilesMatch for specific file types:
Applies caching rules only to the specified file extensions, improving efficiency.
Preventing Hotlinking (Optional):
The provided mod_rewrite rule prevents other websites from directly linking to your images, saving bandwidth. Replace yourdomain.com with your actual domain.
mod_deflate for compression:
Enables Gzip compression for text-based assets, significantly reducing file sizes and improving page load times.
It's important to compress the correct file types.
Clearer organization:
The code is organized into logical sections for easier understanding and modification.
More comprehensive file types:
Added webp, svg, and font file types.
Legacy considerations:
Added legacy javascript mime types.
Important considerations:

Enable the modules: Ensure that mod_expires, mod_headers, mod_deflate, and mod_rewrite are enabled on your Apache server.
Version your assets: For long-term caching to be effective, version your static assets (e.g., style.12345.css). When you update a file, change its version number.
CDN integration: If you use a CDN, configure its caching settings to complement your .htaccess rules.
Testing: Thoroughly test your caching configuration to ensure it's working as expected. Use browser developer tools or online tools like Google PageSpeed Insights.
Placement: Place the .htaccess file in your website's root directory.
Security: Be mindful of the security implications of .htaccess rules. Avoid overly permissive settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment