Aliasディレクティブ:特定のパスへのリクエストに対してドキュメントルート以外のディレクトリを割り当てる

Alias ディレクティブは、クライアントからのリクエストに含まれるパスが特定のパスと先頭から一致した場合、ドキュメントルート以外のディレクトリからクライアントへページを返すように設定するのに使用します。ここでは Apache の Alias ディレクティブの使い方について解説します。

(Last modified: )

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

Alias ディレクティブはリクエストのパス部分が特定のパスと先頭から一致する場合に、ドキュメントルート以外のディレクトリからページを返すように設定します。書式は次のとおりです。

Alias URLパス ファイルパス

記述できる場所は httpd.conf, VirtualHost です。

例えばクライアントからのリクエストに含まれるパスが、先頭から /data と一致した場合にドキュメントルートではなく d:\datadir からクライアントにファイルを返すように設定するには次のように記述します。

Alias /data d:/datadir

この場合、リクエストに対して次のパスにあるファイルを返します。

リクエスト:
http://www.example.com/data/index.html
返すファイル:
d:\datadir\index.html

リクエスト:
http://www.example.com/data/img/image01.png
返すファイル:
d:\datadir\img\image01.png

httpd.conファイルでの記述

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

<IfModule alias_module>
    #
    # Alias: Maps web paths into filesystem paths and is used to
    # access content that does not live under the DocumentRoot.
    # Example:
    # Alias /webpath /full/filesystem/path
    #
    # If you include a trailing / on /webpath then the server will
    # require it to be present in the URL.  You will also likely
    # need to provide a <Directory> section to allow access to
    # the filesystem path.
</IfModule>

Alias ディレクティブの設定は alias_module モジュールが使用可能な場合に行います。具体的な設定手順はこのあと解説します。

Aliasディレクティブを設定する

それでは実際に Alias ディレクティブを設定してみます。 httpd.conf ファイルを開き、次のように記述されている箇所を検索してください。

<IfModule alias_module>
    #
    # Alias: Maps web paths into filesystem paths and is used to
    # access content that does not live under the DocumentRoot.
    # Example:
    # Alias /webpath /full/filesystem/path
    #
    # If you include a trailing / on /webpath then the server will
    # require it to be present in the URL.  You will also likely
    # need to provide a <Directory> section to allow access to
    # the filesystem path.
</IfModule>

Alias ディレクトリに関する設定を記述します。(コメントの部分は省略しています)。

<IfModule alias_module>

  Alias /data d:/datadir

</IfModule>

また Alias ディレクティブで設定したディレクトリに対して全てのアクセスを許可します。

<IfModule alias_module>

  Alias /data d:/datadir

  <Directory d:/datadir>
    Require all granted
  </Directory>

</IfModule>

念のため alias_module モジュールが使用可能になっていることを確認してください。 httpd.conf に次の記述があれば使用可能です。

#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
#LoadModule access_compat_module modules/mod_access_compat.so

LoadModule alias_module modules/mod_alias.so

httpd.conf ファイルを保存します。そのあとで Apache を再起動して設定ファイルを読み込みなおしてください。

次に Alias ディレクトリに設定したファイルパスにファイルを設置します。

Aliasディレクティブを設定する(1)

ブラウザから http://localhost/data/hello.html へアクセスします。 Alias ディレクティブに設定したディレクトリに設置した hello.html ファイルがクライアントへ返されて次のようにブラウザに表示されました。

Aliasディレクティブを設定する(2)

このとき「Forbidden You don't have permission to access this resource.」が表示される場合は、ファイルパスに指定したディレクトリに対してアクセスする権限が設定されているかどうか確認されてください。

Aliasディレクティブを設定する(3)

-- --

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

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

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