Redirectディレクティブ:特定のパスへのリクエストに対してリダイレクトする

Redirect ディレクティブはクライアントからのリクエストに含まれるパスが特定のパスと一致した場合、指定した URL へリダイレクトする設定を行う場合に使用します。ここでは Apache の Redirect ディレクティブの使い方について解説します。

(Last modified: )

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

Redirect ディレクティブはリクエストのパスが特定のパスと先頭から一致する場合に、別の URL へリダイレクトする設定を行います。書式は次のとおりです。

Redirect [ステータス] URLパス URL

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

例えばクライアントからのリクエストに含まれるパスが、先頭から /data と一致した場合に http://www.example.com/data へリダイレクトするには次のように記述します。

Redirect /data http://www.example.com/newdata

この場合、リクエストに対して次の URL へリダイレクトされます。

リクエスト:
http://(元のホスト名)/data/index.html
リダイレクト先:
http://www.example.com/newdata/index.html

http://(元のホスト名)/data/img/image01.png
http://www.example.com/newdata/img/image01.png

リダイレクト先が同じサーバ内の場合は、リダイレクト先の URL をルートディレクトリからの絶対パスで指定することもできます。例えば /data へのリクエストを /newdata へリダイレクトするには次のように記述します。

Redirect /data /newdata

この場合、リクエストに対して次の URL へリダイレクトされます。

リクエスト:
http://(元のホスト名)/data/index.html
リダイレクト先:
http://(元のホスト名)/newdata/index.html

http://(元のホスト名)/data/img/image01.png
http://(元のホスト名)/newdata/img/image01.png

ステータスコードの設定

Redirect ディレクティブでは HTTP レスポンスステータスコードを指定することもできます。ステータスコードは 300 番台がリダイレクトの際に使用するものですが、他の番号も指定可能なようです。ただし 300 番台以外を明示的に指定する場合はリダイレクト先の URL を指定しないでください。

リダイレクトのときによく使用されるステータスコードには 301 と 302 があります。 301 の場合は恒久的にページが変更になったことを表し、 302 の場合は一時的に変更しているがいずれは元に戻すことを表します。

301 恒久的なリダイレクト 302 一時的なリダイレクト

なおステータスコードを省略した場合は 302 が指定されたものとみなされます。またコードの代わりに permanent (301の代わり) や temp (302の代わり) を使って指定することもできます。

Redirect 301 /data http://www.example.com/newdata
Redirect temp /data http://www.example.com/newdata

httpd.conファイルでの記述

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

<IfModule alias_module>
    #
    # Redirect: Allows you to tell clients about documents that used to 
    # exist in your server's namespace, but do not anymore. The client 
    # will make a new request for the document at its new location.
    # Example:
    # Redirect permanent /foo http://www.example.com/bar
</IfModule>

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

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

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

<IfModule alias_module>
    #
    # Redirect: Allows you to tell clients about documents that used to 
    # exist in your server's namespace, but do not anymore. The client 
    # will make a new request for the document at its new location.
    # Example:
    # Redirect permanent /foo http://www.example.com/bar
</IfModule>

Redirect ディレクトリに関する設定を記述します。今回は同じサーバ内の別のディレクトリへリダイレクトします。(コメントの部分は省略しています)。

<IfModule alias_module>
  Redirect /old /new
</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 を再起動して設定ファイルを読み込みなおしてください。

ブラウザから http://localhost/old/hello.html へアクセスします。リクエストに含まれるパスが Redirect ディレクティブで設定した URL パスと一致していますので http://localhost/new/hello.html へリダイレクトされます。

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

-- --

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

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

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