AddTypeディレクティブ:ファイルの拡張子をMIMEタイプに関連付ける

AddType ディレクティブはファイルの拡張子を MIME タイプに関連付けるのに使用します。 Apache ではあらかじめいくつかの拡張子に対して MIME タイプが関連付けされたファイルが用意されていますが、 AddType ディレクティブを使うことで拡張子に対する MIME タイプの関連付けを追加したり上書きできます。ここでは Apache の AddType ディレクティブの使い方について解説します。

(Last modified: )

AddTypeディレクティブの使い方

AddType ディレクティブはファイルの拡張子にコンテンツタイプを関連付けします。書式は次のとおりです。

AddType MIMEタイプ 拡張子 [拡張子] ...

記述できる場所は httpd.conf, VirtualHost, Directory, .htaccess です。 .htaccess で設定する場合は AllowOverride FileInfo が設定されている必要があります。

MIME タイプに関連付ける拡張子を指定してください。 1 つの MIME タイプに対して複数の拡張子を関連付けることができます。

例えば拡張子 .xml に対して MIME タイプ text/xml を関連付ける場合は次のように記述します。

AddType text/xml .xml

※ 拡張子はドット(.)があってもなくても構いません。

httpd.conファイルでの記述

httpd.conf ファイルにはデフォルトで次のように記述されています。

<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig conf/mime.types

    #
    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    #
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
</IfModule>

AddType ディレクティブの設定は mime_module モジュールが使用可能な場合に行います。デフォルトで 2 つの AddType ディレクティブが記述されています。

デフォルトで登録されている拡張子とMIMEタイプの組み合わせ

Apache ではデフォルトでいくつかの MIME タイプと拡張子の組み合わせがファイルに記述されています。どのファイルを読み込んでデフォルトの設定として使用するのかは TypesConfig ディレクティブを使って設定するのですが、先ほどの AddType ディレクティブが記述されていた場所と同じところで設定されています。

<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig conf/mime.types

    #
    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    #
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
</IfModule>

TypesConfig ディレクティブには conf/mime.types ファイルが設定されています。

AddTypeディレクティブの使い方(1)

ファイルはテキストファイルなのでテキストエディタなどでファイルの中身を確認できます。一部抜粋します。

application/zip   zip
audio/midi        mid midi kar rmi
image/jpeg        jpeg jpg jpe
image/png         png
text/html         html htm
text/plain        txt text conf def list log in
video/mp4         mp4 mp4v mpg4

conf/mime.types ファイルに登録されている MIME と拡張子の組み合わせは別途指定する必要はありません。追加を行いたい場合や、現在の設定を上書きしたい場合に AddType ディレクティブを使用してください。

-- --

Apache の AddType ディレクティブの使い方について解説しました。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。