Performance Optimization with .htaccess

Updated November 2026 for Apache 2.4. The complete, current reference is Caching and Compression in THE Ultimate Htaccess.

Optimizing web performance is crucial for improving user experience and reducing server load. Apache’s .htaccess files offer a variety of tools to enhance performance. Here are some key techniques:

1. Enable Compression

Compressing files reduces their size, speeding up download times. Brotli first, gzip as the fallback; each is only used when the browser supports it.

<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/javascript application/javascript application/json image/svg+xml
</IfModule>
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json image/svg+xml
</IfModule>

2. Leverage Browser Caching

Specify caching rules to store static resources locally.

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType image/avif "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType font/woff2 "access plus 1 year"
</IfModule>

3. Gzip Compression

Compress other text-based formats too. Leave images, video and WOFF2 off: they are already compressed.

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE application/xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE application/manifest+json application/wasm
    AddOutputFilterByType DEFLATE font/ttf font/otf
</IfModule>

4. Avoid Image Hotlinking

Prevent other sites from using your images, saving bandwidth.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F]

Referers are easy to strip, so this also lets through some hotlinkers. A version that uses the browser's Sec-Fetch-Site header instead is in the 2026 guide.

5. Enable Keep-Alive

Keep-alive and HTTP/2 are server settings, and Apache 2.4 already has keep-alive on by default. They cannot be enabled from .htaccess. Setting a Connection header there does nothing useful, and HTTP/2 forbids connection-specific headers. If you control the server:

# Server config (httpd.conf), not .htaccess
KeepAlive On
KeepAliveTimeout 5
Protocols h2 http/1.1

6. Optimize File Delivery

Files with a content hash in the name never change, so cache them for a year and mark them immutable. Leave ETags alone: without them, every expired file is downloaded again in full instead of getting a cheap 304.

<FilesMatch "\.[0-9a-f]{8,}\.(css|js|mjs|woff2|avif|webp|png|jpe?g|svg)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>

Conclusion

Using .htaccess files for performance optimization can significantly enhance website speed and efficiency. Implement these techniques to improve load times, reduce server load, and deliver a smoother user experience. Regularly review and update your .htaccess rules to adapt to evolving performance best practices.

Comments