【CakePHP】mod_rewriteのルールに従っていないファイルにもアクセスしたい
CakePHPのフォーラムでの質問に興味をそそられて
自分なりにいろいろ調べてみました。
【目標】
下記を同時に実現する。- http://example.com/index.html(非Cake)
- http://example.com/hoge/fuga.html(非Cake)
- http://example.com/controller/action(Cake)
【結論】
Cakeフォルダ直下の『.htaccess』をいじることで解決しました。
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ index.html [L]
RewriteRule ^([^/]+\..+)$ $1 [L]
RewriteRule ^hoge/?$ hoge/index.html [L]
RewriteRule ^hoge/(.*) hoge/$1 [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
【解説】
RewriteRule ^$ index.html [L]http://example.com/のように何も指定がない場合は
http://example.com/index.htmlを表示。
RewriteRule ^([^/]+\..+)$ $1 [L]http://example.com/hoge.htmlや、
http://example.com/fuga.html?q=searchなど、
ルート直下のファイルを指定している場合に対応。
RewriteRule ^hoge/?$ hoge/index.html [L]http://example.com/hoge/など、
コントローラではない、特定のフォルダを指定された場合に対応。
RewriteRule ^hoge/(.*) hoge/$1 [L]http://example.com/hoge/test.htmlなど、
コントローラではない、特定のフォルダ内のファイルの指定に対応。
RewriteRule (.*) app/webroot/$1 [L](変更前の記述のまま)
上記の条件に当てはまらず、http://example.com/の後に
記述が続く場合は、『app/webroot/』下へ飛ぶ。
【参考】CakePHP内の処理を理解する(2/3):CodeZine
http://codezine.jp/article/detail/768?p=2