.htaccess FilesUpdated November 2026 for Apache 2.4. The complete, current reference is MIME Types the Old Guide Never Heard Of in THE Ultimate Htaccess.
Managing MIME types and content handling in .htaccess files is crucial for ensuring that web content is delivered correctly and securely. MIME types inform the browser about the type of content being served, allowing it to handle the content appropriately.
You can define custom MIME types in your .htaccess file to ensure that files are processed correctly by the browser.
Example:
AddType image/avif .avif
AddType image/webp .webp
AddType font/woff2 .woff2
AddType text/javascript .js .mjs
AddType application/wasm .wasm
AddType application/manifest+json .webmanifest
In this example:
mime.types files. A browser will refuse a JavaScript module or a font served with the wrong type.text/javascript is the official type for JavaScript (RFC 9239), for both .js and .mjs.AddType for .swf or .flv.To specify the character encoding for your files, use the AddDefaultCharset directive.
Example:
AddDefaultCharset UTF-8
AddDefaultCharset only adds charset=UTF-8 to text/html and text/plain responses. For CSS, JavaScript and the rest, add AddCharset UTF-8 .css .js .mjs .json .svg.
MIME type sniffing can be a security risk as it allows browsers to infer the content type of files. To prevent this, you can set the X-Content-Type-Options header to nosniff.
Example:
Header always set X-Content-Type-Options "nosniff"
This header instructs browsers not to try to guess the MIME type, ensuring they rely on the provided Content-Type header.
The Content-Disposition header controls how content is presented (inline or as an attachment).
Example:
<FilesMatch "\.(pdf|zip)$">
Header set Content-Disposition attachment
</FilesMatch>
This example sets the Content-Disposition to attachment for PDF and ZIP files, prompting the browser to download these files rather than displaying them inline.
Compression reduces the size of text files sent to the client. Brotli compresses smaller than gzip. Enable both: each is only used when the browser says it supports it.
Example:
<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>
Leave images, video and WOFF2 off the list. They are already compressed, so compressing them again only burns CPU.
Configuring MIME types and content handling in .htaccess files is essential for ensuring that content is delivered correctly and securely. By setting appropriate MIME types, enforcing character encoding, preventing MIME type sniffing, controlling content disposition, and enabling Gzip compression, you can significantly enhance the performance and security of your web applications. Regularly review and update your .htaccess configurations to keep up with best practices and emerging standards.