installing cakephp 2.0.0 in a subdirectory

2019-06-05 03:37发布

问题:

i found a similar problem here but did not find a solution. basically i have installed an existing cakephp application in a subdirectory 'localhost/dyod'. without editing the htaccess files, i get an error telling me that the app is looking for the dyod controller, so seemingly the uri is off somehow. when i add a rewritebase to the htaccess files (/dyod/) then all the assets' urls are off and don't include the webroot. also, it is still looking for a dyod controller. my htaccess files are copied from here.

thanks in advance.

回答1:

Adding on to this since it was at the top of my google search and these answers didn't help.

CakePHP uses 3 .htaccess files that all need modifying. One in the top CakePHP dir, one in app/ and one in app/webroot. Add the following to each of these:

RewriteBase /path/to/dir/

On my machine I have the following structure: ~/Sites/site1 ~/sites/site2/admin

Each of those directories have a seperate install of CakePHP.

With CakePHP installed in site1 and in site2/admin. The urls for these sites are:

http://localhost/~me/site1
http://localhost/~me/site2/admin

I kept getting this error: The requested URL /home/me/Sites/site1/app/webroot/index.php was not found on this server.

I modified the 3 .htaccess files to get these:

site1 .htaccess:

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

site1/app .htaccess:

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

site1/app/webroot .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /~me/site1/app/webroot/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

That's it. Those three changes are all you need.



回答2:

In your .htaccess files add the RewriteBase directive:

RewriteBase /path/to/cake/app

In your case this would be:

RewriteBase /path/to/dyod



回答3:

This page explains how to install CakePHP in a different than "normal" location:

http://book.cakephp.org/2.0/en/installation/advanced-installation.html

Basically, you just change 3 values in your webroot/index.php file. You should not have to edit any .htaccess files. I install CakePHP in subdirectories for every site I do now, and I've never had to touch an .htaccess file. Just change the values in webroot/index.php and you're good to go.

Example - My directory structure:

-cakephp
    -cakephp_2_2
    -cakephp_1_3
    -cakephp_2_1_beta
    - ...etc
-public_html
    -mysite1
        -Config
        -Console
        -Controller
        -Lib
        - ...etc
        -View
        -webroot
    -mysite2
        -Config
        -Console
        -Controller
        -Lib
        - ...etc
        -View
        -webroot

In my webroot:

if (!defined('ROOT')) {
define('ROOT', DS.'home'.DS.'myusername'.DS.'public_html');
}


if (!defined('APP_DIR')) {
define('APP_DIR', 'mysite1');
}


define('CAKE_CORE_INCLUDE_PATH', DS.'home'.DS.'myusername'.DS.'cakephp'.DS.'cakephp_2_2'.DS.'lib');