[CakePHP] インストールディレクトリの.htaccessだけではなく、appディレクトリとapp/webrootディレクトリの.htaccessも修正する必要がある。

環境

この記事で対象となっている環境は、Ubuntu 6.10, Apache 2.0.55, PHP 5.1.6, CakePHP 1.1.11.4064です。

問題の現象

Instalilng CakePHPにしたがってCakePHPを/home/foo/public_html/cakeにインストールし、/home/foo/public_html/cake/.htaccessにRewriteBaseも追加したが、http://example.com/~foo/cake/items/indexなどにアクセスすると、"404 Not Found"となる。

ウェブサーバのメッセージには、

The requested URL /home/foo/public_html/cake/app/webroot/index.php was not found on this server.

とあり、index.php絶対パスが表示される(通常、/~foo/barがない場合は、

The requested URL /~foo/bar was not found on this server.

のように、絶対パスが表示されることはない)。

また、Apacheのエラーログには、

[Tue Dec 26 02:37:59 2006] [error] [client 127.0.0.1] File does not exist: /var/www/home

のように、サーバのドキュメントルートでhomeディレクトリを探したと思われるエラーが追加されている。

対策

1. インストールディレクトリの.htaccessを確認する。CakePHPのドキュメントにしたがっていれば、以下の通りになっているはずである。

$ cat .htaccess                             [~/public_html/cake]

   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
   RewriteBase    /~foo/cake

2. インストールディレクトリの下のappディレクトリの.htaccessに、RewriteBaseを追加する。

$ cat .htaccess                         [~/public_html/cake/app]

    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
    RewriteBase    /~foo/cake/app
 

3. app/webrootディレクトリの.htaccessに、RewriteBaseを追加する。

$ cat .htaccess                 [~/public_html/cake/app/webroot]

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    RewriteBase /~foo/cake/app/webroot

現象の詳細

Apache2の設定で、

$ grep Rewrite /etc/apache2/apache2.conf    [~/public_html/cake]
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 9

とし、mod_rewriteのログを出力させると、以下の様になる。

127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (3) [per-dir /home/foo/public_html/cake/] add path info postfix: /home/foo/public_html/cake/items -> /home/foo/public_html/cake/items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (3) [per-dir /home/foo/public_html/cake/] strip per-dir prefix: /home/foo/public_html/cake/items/list -> items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (3) [per-dir /home/foo/public_html/cake/] applying pattern '^$' to uri 'items/list'
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (3) [per-dir /home/foo/public_html/cake/] add path info postfix: /home/foo/public_html/cake/items -> /home/foo/public_html/cake/items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (3) [per-dir /home/foo/public_html/cake/] strip per-dir prefix: /home/foo/public_html/cake/items/list -> items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (3) [per-dir /home/foo/public_html/cake/] applying pattern '(.*)' to uri 'items/list'
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (2) [per-dir /home/foo/public_html/cake/] rewrite items/list -> app/webroot/items/list   
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (3) [per-dir /home/foo/public_html/cake/] add per-dir prefix: app/webroot/items/list -> /home/foo/public_html/cake/app/webroot/items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (2) [per-dir /home/foo/public_html/cake/] trying to replace prefix /home/foo/public_html/cake/ with /~foo/cake
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (5) strip matching prefix: /home/foo/public_html/cake/app/webroot/items/list -> app/webroot/items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (4) add subst prefix: app/webroot/items/list -> /~foo/cake/app/webroot/items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#826e430/initial] (1) [per-dir /home/foo/public_html/cake/] internal redirect with /~foo/cake/app/webroot/items/list [INTERNAL REDIRECT]
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (3) [per-dir /home/foo/public_html/cake/app/webroot/] add path info postfix: /home/foo/public_html/cake/app/webroot/items -> /home/foo/public_html/cake/app/webroot/items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (3) [per-dir /home/foo/public_html/cake/app/webroot/] strip per-dir prefix: /home/foo/public_html/cake/app/webroot/items/list -> items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (3) [per-dir /home/foo/public_html/cake/app/webroot/] applying pattern '^(.*)$' to uri 'items/list'
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (4) RewriteCond: input='/home/foo/public_html/cake/app/webroot/items' pattern='!-d' => matched
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (4) RewriteCond: input='/home/foo/public_html/cake/app/webroot/items' pattern='!-f' => matched
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (2) [per-dir /home/foo/public_html/cake/app/webroot/] rewrite items/list -> index.php?url=items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (3) split uri=index.php?url=items/list -> uri=index.php, args=url=items/list
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (3) [per-dir /home/foo/public_html/cake/app/webroot/] add per-dir prefix: index.php -> /home/foo/public_html/cake/app/webroot/index.php
127.0.0.1 - - [26/Dec/2006:03:15:22 +0900] [127.0.0.1/sid#814b3e8][rid#827a018/initial/redir#1] (1) [per-dir /home/foo/public_html/cake/app/webroot/] internal redirect with /home/foo/public_html/cake/app/webroot/index.php [INTERNAL REDIRECT]

ログを見る限り、/home/foo/public_html/cake/app/webroot/まで処理が進んでいることが確認された。そこで、/home/foo/public_html/cake/app/webrootの.htaccessと、/home/foo/public_html/cake/app/.htaccessにRewriteBaseを追加して、問題は解決された。