mod_rewriteを使ってリクエストを制御する練習。
とりあえずよく使いそうな「ファイルの拡張子を消す」処理を実現してみる。
今回対象とするのは「html」拡張子。
必要なモジュールの確認をする
(1) rewrite_module
$sudo apache2ctl -M | grep rewrite
→ 見つからない
インストールをする前に、組み込まれていないだけでインストールされていないかを確認する
$ ll /usr/lib/apache2/modules/ | grep rewrite -rw-r--r-- 1 root root 63488 Jul 12 22:38 mod_rewrite.so
→ 両方見つかった
rewriteモジュールの有効化
$ sudo a2enmod rewrite Enabling module dav. To activate the new configuration, you need to run: service apache2 restart
apache再起動
$ sudo service apache2 restart * Restarting web server apache2 ...
apache2.confの設定
$sudo -H vim /etc/apache2/apache2.conf
※ 以下の内容を設定を反映させたいDirectoryディレクティブ配下に記述
RewriteEngine On RewriteRule ^/(.*)$/$1.html [L]
文法チェック
$sudo apachectl -t Syntax OK
ブラウザで確認してみる
あらかじめ作っておいたindex.htmlとtest.htmlで実験
http://192.168.56.101/index.html → OK
http://192.168.56.101/test.html → OK
http://192.168.56.101/index → OK
http://192.168.56.101/test → OK
→ 拡張子無しでもアクセスできた!
mod_rewrite、かなり色々なことができそうなので徐々に幅を広げていきたい。
※ 参考
http://tech.bayashi.jp/archives/entry/techweb/2007/001981.html
http://blog.dawgsdk.org/weblog/archives/411011
http://d.hatena.ne.jp/tilfin/20071022/1194390269