kawama.jp

mod_rewriteでアクセスごとに処理を振り分ける

Filed under: Apache — 2006年9月20日 16:26 — Comments (0)hatebuhatebu count

今回やったことは、

hoge.example.com へアクセスがあった場合 /home/homepage/public_html/hoge に

ahe.example.com へアクセスがあった場合 /home/homepage/public_html/ahe に

それぞれ処理を振り分ける、というもの。もちろん「hoge」と「ahe」だけじゃなくて「ufu」でも「oho」でもなんでもあり、/home/homepage/public_html以下にディレクトリが存在していればそちらに処理を振り分けます。

mod_rewriteを使い、httpd.confに以下のように記述します。

    ServerAdmin root@example.com

    DocumentRoot /home/homepage/public_html

    ServerName www.example.com

    ServerAlias *.example.com

    ErrorLog logs/example-error_log

    CustomLog logs/example-access_log combined



    RewriteEngine On

    #RewriteLog logs/test-rewrite.log

    #RewriteLogLevel 3

    RewriteCond %{HTTP_HOST}  ([^\.]+).*

    RewriteRule ^(.*)$ /%1/$1 [L]

※当然ですが、apacheの設定以前にDNSにワイルドカードで登録しておく必要があります。

以上で完了です。なお、mod_rewriteを使わずにphp等でスクリプトを書いて処理を振り分けるやり方もいいと思います。今回は勉強がてらmod_rewriteを使ってみました。 

参考

http://www.net-newbie.com/trans/mod_rewrite.html

http://tech.bayashi.net/svr/doc/apache/mod_rewrite.html

http://www.kobarin.jp/blog/MT3/archives/2005/01/virtualhost_www.html

http://webcraft.jp/asy/linux/mod_rewrite.htm

コメントはまだありません »

コメントはまだありません。

この投稿へのコメントの RSS フィード。 TrackBack URL

コメントする

Copyright (C) 2002 - 2010 kawama.jp. All Rights Reserved. — Powered by WordPress