<-
Apache > HTTP サーバ > ドキュメンテーション > バージョン 2.5 > Rewrite

mod_rewrite を使った動的な大量バーチャルホスト

翻訳済み言語:  de  |  en  |  es  |  fr  |  ja  |  ko  |  tr  |  zh-cn 

このドキュメントは mod_rewrite リファレンスドキュメントを補足するものです。 mod_rewrite を使用して動的に設定されるバーチャルホストを 作成する方法を説明します。

mod_rewrite は通常、バーチャルホストを 設定する最良の方法ではありません。mod_rewrite に頼る前に、まず 代替手段を検討してください。 「mod_rewrite を避ける方法」ドキュメントも 参照してください。

参照

top

任意のホスト名に対するバーチャルホスト

説明:

ドメイン内で解決されるすべてのホスト名に対して、新しい VirtualHost セクションを作成することなく、自動的にバーチャルホストを 作成したいとします。

このレシピでは、各ユーザに対してホスト名 SITE.example.com を使用し、 コンテンツを /home/SITE/www から 提供すると仮定しています。ただし、www.example.com は このマッピングから除外したいとします。

解決方法:
RewriteEngine on

RewriteMap    lowercase int:tolower

RewriteCond   %{HTTP_HOST} !^www\.
RewriteCond   ${lowercase:%{HTTP_HOST}}   ^([^.]+)\.example\.com$
RewriteRule   ^(.*)    /home/%1/www$1
議論
DNS 解決に注意する必要があります - Apache は 名前解決を処理しません。各ホスト名の CNAME レコードか、DNS ワイルドカードレコードを作成する必要があります。DNS レコードの 作成はこのドキュメントの範囲外です。

内部 tolower RewriteMap ディレクティブは、使用される ホスト名がすべて小文字であることを保証し、作成すべきディレクトリ構造に 曖昧さがないようにします。

RewriteCond で使用される 括弧はバックリファレンス %1%2 等に キャプチャされ、RewriteRule で使用される括弧はバックリファレンス $1$2 等にキャプチャされます。

最初の RewriteCond は、ホスト名が www. で 始まるかどうかを確認し、始まる場合は書き換えがスキップされます。

このドキュメントで説明されている多くのテクニックと同様に、 mod_rewrite はこのタスクを達成する最良の方法では ありません。代わりに mod_vhost_alias の使用を検討して ください。静的ファイルの提供以外のこと、例えば動的コンテンツや Alias の解決をはるかに優雅に処理できます。

top

mod_rewrite を使った動的バーチャルホスト

この httpd.conf からの抜粋は、 最初の例と同じことを行います。 前半は上記の対応する部分と非常に似ていますが、後方互換性のため、 および mod_rewrite 部分が正しく動作するように いくつかの変更が加えられています。後半は mod_rewrite に実際の作業を設定します。

mod_rewrite は他の URI 変換モジュール (例: mod_alias) よりも前に実行されるため、 それらのモジュールで処理されるはずの URL を明示的に無視するように mod_rewrite に指示する必要があります。また、これらの ルールは ScriptAlias ディレクティブをバイパスするため、 mod_rewrite にそれらのマッピングを明示的に実行させる 必要があります。

# get the server name from the Host: header
UseCanonicalName Off

# splittable logs
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog "logs/access_log" vcommon

<Directory "/www/hosts">
    # ExecCGI is needed here because we can't force
    # CGI execution in the way that ScriptAlias does
    Options FollowSymLinks ExecCGI
</Directory>

RewriteEngine On

# a ServerName derived from a Host: header may be any case at all
RewriteMap  lowercase  "int:tolower"

## deal with normal documents first:
# allow Alias /icons/ to work - repeat for other aliases
RewriteCond  "%{REQUEST_URI}"  "!^/icons/"
# allow CGIs to work
RewriteCond  "%{REQUEST_URI}"  "!^/cgi-bin/"
# do the magic
RewriteRule  "^/(.*)$"         "/www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1"

## and now deal with CGIs - we have to force a handler
RewriteCond  "%{REQUEST_URI}"  "^/cgi-bin/"
RewriteRule  "^/(.*)$"         "/www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1"  [H=cgi-script]
top

個別のバーチャルホスト設定ファイルの使用

この構成では、mod_rewrite のより高度な機能を使用して、 個別の設定ファイルからバーチャルホストのドキュメントルートへの 変換を行います。これにより柔軟性が高まりますが、より複雑な設定が 必要になります。

vhost.map ファイルは次のようになります:

customer-1.example.com /www/customers/1
customer-2.example.com /www/customers/2
# ...
customer-N.example.com /www/customers/N

httpd.conf には以下を含める必要があります:

RewriteEngine on

RewriteMap   lowercase  "int:tolower"

# define the map file
RewriteMap   vhost      "txt:/www/conf/vhost.map"

# deal with aliases as above
RewriteCond  "%{REQUEST_URI}"               "!^/icons/"
RewriteCond  "%{REQUEST_URI}"               "!^/cgi-bin/"
RewriteCond  "${lowercase:%{SERVER_NAME}}"  "^(.+)$"
# this does the file-based remap
RewriteCond  "${vhost:%1}"                  "^(/.*)$"
RewriteRule  "^/(.*)$"                      "%1/docs/$1"

RewriteCond  "%{REQUEST_URI}"               "^/cgi-bin/"
RewriteCond  "${lowercase:%{SERVER_NAME}}"  "^(.+)$"
RewriteCond  "${vhost:%1}"                  "^(/.*)$"
RewriteRule  "^/cgi-bin/(.*)$"                      "%1/cgi-bin/$1" [H=cgi-script]

翻訳済み言語:  de  |  en  |  es  |  fr  |  ja  |  ko  |  tr  |  zh-cn