Configuring MIME Types
1. Understanding MIME Type Configuration
| Directive | Description | Default | Notes |
|---|---|---|---|
| AddType | Maps a file extension to a MIME type | None | Can be used multiple times for different extensions |
| AddHandler | Maps a file extension to a handler | None | Used for server-side processing of files |
| TypesConfig | Specifies the location of the MIME types configuration file | /etc/mime.types | Can be overridden to use a custom file |
Note: MIME types are used by the server to determine how to serve files to clients. Proper configuration ensures that browsers handle files correctly.
2. Setting Default MIME Type
| Directive | Values | Description |
|---|---|---|
| DefaultType | text/plain |
Sets the default MIME type for files without an extension or when content negotiation fails |
Note: Setting
DefaultType to application/octet-stream ensures that files without a recognized extension are treated as binary data.
3. Adding MIME Types
| Directive | Values | Description |
|---|---|---|
| AddType | mime/type file-extension |
Adds a MIME type for a specific file extension |
| AddHandler | handler-name file-extension |
Binds a file extension to a specific handler |
| AddOutputFilter | filter-name file-extension |
Adds an output filter to files with a specific extension |
Note: MIME types should be registered with IANA or defined locally for internal use.
4. Mapping File Extensions
| Extension | MIME Type | Description |
|---|---|---|
| .html | text/html |
HTML document |
| .css | text/css |
Cascading Style Sheet |
| .js | application/javascript |
JavaScript file |
| .json | application/json |
JSON data |
| .xml | application/xml |
XML document |
| .jpg | image/jpeg |
JPEG image |
| .png | image/png |
PNG image |
| .gif | image/gif |
GIF image |
| .svg | image/svg+xml |
SVG vector image |
application/pdf |
Portable Document Format | |
| .txt | text/plain |
Plain text file |
| .mp4 | video/mp4 |
MP4 video |
| .webm | video/webm |
WebM video |
| .ogg | audio/ogg |
Ogg audio |
| .mp3 | audio/mpeg |
MPEG audio |
| .wav | audio/wav |
WAV audio |
| .ico | image/x-icon |
Icon file |
| .woff | font/woff |
Web Open Font Format |
| .woff2 | font/woff2 |
Web Open Font Format 2 |
| .eot | application/vnd.ms-fontobject |
Embedded OpenType font |
5. Using MIME Types File
Apache HTTP Server uses MIME types to determine how to handle different file extensions. The mime.types file is used to define these mappings.
| MIME Type | File Extension | Description |
|---|---|---|
| text/html | .html, .htm |
HTML documents |
| text/css | .css |
Cascading Style Sheets |
| application/javascript | .js |
JavaScript files |
| image/jpeg | .jpg, .jpeg |
JPEG images |
| image/png | .png |
PNG images |
| application/json | .json |
JSON data |
| text/plain | .txt |
Plain text files |
Note: The
AddType directive can be used to associate a MIME type with one or more file extensions.
6. Configuring Character Set
| Directive | Values | Description |
|---|---|---|
| AddDefaultCharset | UTF-8 |
Sets the default character set for all responses |
| AddCharset | UTF-8 |
Adds a character set to the Content-Type header for specific file extensions |
Note: Ensure that the character set matches the encoding of your content to prevent display issues.
7. Setting Language Headers
| Directive | Description | Default | Notes |
|---|---|---|---|
| AddLanguage | Sets the language for a file extension | None | Can be used multiple times to define language mappings |
| LanguagePriority | Defines the order of language selection | None | Used in conjunction with AddLanguage |
Example: Setting Language Headers
AddLanguage en .en
AddLanguage fr .fr
LanguagePriority en fr
<IfModule mod_mime.c>
AddLanguage en .en
AddLanguage fr .fr
</IfModule>
Note: The
AddLanguage directive is used to associate file extensions with language codes. These language codes can then be used in content negotiation.
8. Configuring Content Encoding
| Directive | Description | Values | Notes |
|---|---|---|---|
| AddEncoding | Maps a content encoding to a file extension or MIME type | encoding file-extension or mimetype |
Can be used multiple times for different encodings |
| RemoveEncoding | Removes a content encoding from a file extension or MIME type | file-extension or mimetype |
Useful to override default encodings |
Note: Content encoding directives are used to specify how content is compressed or encoded before being sent to the client.
9. Managing Custom MIME Types
| MIME Type | File Extension | Description | Notes |
|---|---|---|---|
| application/json | .json |
JavaScript Object Notation | Used for data exchange |
| text/xml | .xml |
Extensible Markup Language | Used for structured data |
| application/pdf | .pdf |
Portable Document Format | Common for documents |
| image/svg+xml | .svg |
Scalable Vector Graphics | Used for vector images |
| application/javascript | .js |
JavaScript code | Used for client-side scripting |
| text/css | .css |
Cascading Style Sheets | Used for styling web pages |
Note: Ensure that the MIME type is correctly associated with its file extension to avoid content-type issues.
10. Using Output Filters
| Filter Name | Description | Module | Available In |
|---|---|---|---|
| deflate | Compresses output using gzip or deflate encoding | mod_deflate |
Apache 2.0+ |
| filter | Generic filter interface for custom filters | mod_filter |
Apache 2.2+ |
| headers | Adds or modifies response headers | mod_headers |
Apache 2.0+ |
Example: Enabling Gzip Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
Example: Adding Custom Headers
<IfModule mod_headers.c>
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "DENY"
Header set X-XSS-Protection "1; mode=block"
</IfModule>
Note: Output filters are applied after content is generated but before it's sent to the client. They can be used for compression, header manipulation, and content transformation.
11. Understanding MIME Type Precedence
| MIME Type Definition Source | Priority Level | Description |
|---|---|---|
| mod_mime | 1 |
Default MIME types defined by the module |
| AddType | 2 |
Explicitly added MIME types via configuration directives |
| AddHandler | 3 |
Handler-based type mappings (lower priority than AddType) |
| mod_mime_magic | 4 |
Detects MIME types based on file content (if enabled) |
| DefaultType | 5 |
Fallback MIME type for files without a defined type |
Example: MIME Type Precedence in Action
<IfModule mod_mime.c>
AddType text/html .shtml
AddType application/json .json
</IfModule>
# The .json file will be served as application/json due to AddType
# even if the default type is text/plain
Note: The
AddType directive has higher precedence than mod_mime_magic, which means that explicitly defined MIME types will override automatic detection.
12. Testing MIME Type Detection
| Header | Value | Description |
|---|---|---|
| Content-Type | text/html |
MIME type detected for .html files |
| Content-Type | application/json |
MIME type for .json files |
Note: The
Content-Type header is set by the server based on file extensions and AddType directives.