CakePHPをサブディレクトリにて運用

CakePHP#cakephp#htaccess#サブディレクトリ

この記事は12年以上前に書かれたものです。内容が古くなっている可能性があります。

CakePHPをサブディレクトリで運用する案件がございましたので備忘録。

http://example.com/test

この場合、testディレクトリにCakePHPを導入するといった件になります。

手順としては以下になります。

まず、testフォルダ(CakePHPを格納したディレクトリ)の.htaccess。

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule  ^$ app/webroot/    [L]
   RewriteRule  (.*) app/webroot/$1 [L]
   RewriteBase  /test
</IfModule>

そして、appディレクトリ直下にある.htaccess。

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ webroot/    [L]
    RewriteRule  (.*) webroot/$1 [L]
    RewriteBase  /test/app
</IfModule>

最後に、webrootディレクトリ直下にある.htaccess。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond  %{REQUEST_FILENAME} !-d
    RewriteCond  %{REQUEST_FILENAME} !-f
    RewriteRule  ^(.*)$ index.php [QSA,L]
    RewriteBase  /test/app/webroot
</IfModule>

これで、testディレクトリでもCakePHPが利用できると思います。