Apache mod rewriteメモ
mod_rewrite
ロギング
httpd.conf
に追加
LogLevel alert rewrite:trace4
ログはerror.log
に吐かれる。
sed -e 's/^\(.*\)\(\[perdir.*\)$/\2/'
RewriteBaseとは
書式
RewriteBase URL上のディレクトリパス
説明
- DocumentRoot
/var/www/site/
- htmlの配置
/var/www/site/abc/hoge.html /var/www/site/abc/piyo.html
- ルール
RewriteEngine On RewriteBase /abc RewriteRule ^abc/hoge\.html piyo.html [L]
- 例: URLを
http://server/abc/hoge.html
と指定した場合、表示されているURLはそのままで、${DOCUMENT_ROOT}${REWRITE_BASE}/piyo.html
の内容が表示される。
この場合、URL表示はhttp://server/abc/hoge.html
で、実際には/var/www/site/abc/piyo.html
が表示される。
Rewriteのメカニズム
Aliasがない場合
httpd.conf
DocumentRoot /var/www/html
/var/www/html/ore/sandbox/.htaccess
RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
リクエストパス:http://example.com/ore/sandbox/aaa/zzz
- URLのパス部分: ore/sandbox/aaa/zzz
mod_rewrite
には、${DocumentRoot}+${URLのパス}
が渡される:/var/www/html/ore/sandbox/aaa/zzz
.htaccess
のあるディレクトリまでのパスを削除/var/www/html/ore/sandbox/aaa/zzz
->aaa/zzz
RewriteRule
条件のチェック^.*$
なのでマッチ
- パスの書き換え
aaa/zzz
->index.php
- 2. で除去したパスをくっつける。
index.php
->/var/www/html/ore/sandbox/index.php
- ドキュメントルートを削除
/var/www/html/ore/sandbox/index.php
->/ore/sandbox/index.php
- 書き換えられたパスに内部リダイレクト
/ore/sandbox/index.php
Aliasを使った時
httpd.conf
DocumentRoot /var/www/html Alias /ore/sandbox /home/ore/sandbox/html
/home/ore/sandbox/.htaccess
RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
リクエストパス:http://example.com/ore/sandbox/aaa/zzz
- URLのパス部分: ore/sandbox/aaa/zzz
mod_rewrite
には、${Aliasで指定したパス}+${URLのパス}
が渡される:/home/ore/sandbox/html/aaa/zzz
.htaccess
のあるディレクトリまでのパスを削除/home/ore/sandbox/html/aaa/zzz
->aaa/zzz
RewriteRule
条件のチェック^.*$
なのでマッチ
- パスの書き換え
aaa/zzz
->index.php
- 2. で除去したパスをくっつける。
index.php
->/home/ore/sandbox/html/index.php
- ドキュメントルートを削除
/home/ore/sandbox/html/index.php
->/home/ore/sandbox/html/index.php
- ところが、パスのプレフィックスが違うために取り除けない。
- 書き換えられたパスに内部リダイレクト
/home/ore/sandbox/html/index.php
- しかしながら、
http://example.com/home/ore/sandbox/html/index.php
というパスは存在しないため、404 Not Found
を返す。
- しかしながら、
RewriteRuleとは
書式
RewriteRule 置換前のパターン 置換後の文字列
- 置換前のパターン:リクエストされたURLとは限らず、書き換えされてきたURLも指す。
説明
- DocumentRoot
/var/www/site/
- htmlの配置
/var/www/site/hoge.html /var/www/site/piyo.html
- ルール
RewriteEngine On RewriteRule ^/?hoge\.html$ piyo.html [L]
^/?hoge\.html$
:渡ってきたURL(正規表現)をpiyo.html
で置換。[L]
はここで書き換えを終了するという意味。
- URLが
hoge.html
と渡ってくるか、/hoge.html
と渡ってくるかの違い<VirtualHost>
ディレクティブの場合は、URLのパスが対象となり、先頭に/
が付く- `Directory`ディレクティブや`.htaccess`の場合は、そのディレクトリからの相対パスが対象となり、先頭に`/`が付かない。
(.*)
パターンをグループ化
RewriteEngine On RewriteRule (.*) http://server/$1 [R]
(.*)
を変数$1
で展開
http://server1/hoge.html -> http://server/hoge.html
RewriteEngine ON RewriteRule ^/旧ディレクトリ/(.*) /新ディレクトリ/$1
RewriteEngine On RewriteRule ^(/somedir/.*) http://newserver/$1 [R]
RwriteRuleのフラグ
フラグ | 意味 | 説明 |
---|---|---|
C | Chain | このルールにマッチしたら、次のルールを評価 |
F | Forbidden | アクセス禁止(403-Forbidden)。[L]フラグと同様、以降のルールは無視。 |
G | Gone | 削除(410-GOne) |
L | Last | マッチしたらRewriteを辞めます。以降のルールは無視。 |
NC | No Case | 大文字小文字を無視 |
NE | No Escape | "."、"?"、"%"などの特殊文字が"%25"のようにエンコードされるのを防ぐ |
OR | Or | RewriteCondを複数指定する場合のOr指定。指定なしならAND |
PT | Path Through | Rewriteを終了し、それ以外の処理に移行 |
R | Redirect | 指定したURLにリダイレクト。[R=303]のようにリダイレクトコードも付加できる。[L]同様、以降のルールは無視。 |