Apache AddType

IT 위키

The AddType directive in Apache HTTP Server is used to define or change the MIME (Multipurpose Internet Mail Extensions) type for specific file extensions. MIME types tell the browser how to handle files received from the server, such as rendering HTML, displaying images, or executing scripts. Setting the correct MIME type is essential for the server to communicate file handling instructions to the client.

Purpose of AddType[편집 | 원본 편집]

The AddType directive helps in setting the MIME type for various file extensions. This can be useful for:

  • Ensuring that web browsers interpret files correctly, such as displaying HTML or running JavaScript.
  • Supporting custom file types, which may not have a predefined MIME type.
  • Overriding default MIME types for specific file extensions to meet specific handling requirements.

Syntax of AddType[편집 | 원본 편집]

The syntax for the `AddType` directive is as follows:

AddType MIME-type file-extension [file-extension] ...
  • MIME-type: The MIME type to associate with the file extension (e.g., `text/html`, `image/png`).
  • file-extension: The file extension to apply the MIME type to (e.g., `.html`, `.png`).

Examples of AddType Usage[편집 | 원본 편집]

Example 1: Setting HTML MIME Type[편집 | 원본 편집]

To set `.html` and `.htm` files to be served as HTML:

AddType text/html .html .htm

This instructs the server to treat files with the `.html` and `.htm` extensions as HTML content.

Example 2: Adding MIME Type for JavaScript[편집 | 원본 편집]

To ensure `.js` files are treated as JavaScript:

AddType application/javascript .js

This directive helps browsers recognize and execute `.js` files correctly as JavaScript.

Example 3: Custom MIME Type for Audio[편집 | 원본 편집]

To add support for custom audio file extensions, such as `.myaudio`:

AddType audio/mpeg .myaudio

This associates the custom `.myaudio` extension with the `audio/mpeg` MIME type, allowing browsers to treat it as an audio file.

Security Considerations[편집 | 원본 편집]

While setting MIME types correctly can enhance functionality, it is important to consider security:

  • Avoid Incorrect MIME Types for Executable Files: Ensure that potentially harmful files are not mistakenly served with an executable MIME type.
  • Use Proper MIME Types for Scripts: Incorrectly setting MIME types for scripts (e.g., `application/javascript` for `.js` files) can cause cross-site scripting vulnerabilities if not managed properly.
  • Restrict Untrusted File Types: Be cautious when defining new MIME types for unknown file extensions, especially in environments where users can upload files.

Related Concepts[편집 | 원본 편집]

Understanding the `AddType` directive involves familiarity with other Apache directives and web concepts:

  • DefaultType: Sets the MIME type for files with unknown extensions if not explicitly defined by `AddType`.
  • AddHandler: Associates specific file types with particular server actions, such as treating files as PHP scripts.
  • MIME Types: The broader concept of file types used by browsers to handle content appropriately.
  • .htaccess: `AddType` can be configured within `.htaccess` files for per-directory MIME type management.

See Also[편집 | 원본 편집]