Let’s say you have a WordPress installation which is accessible in the browser at example.com and you want to install CakePHP in a sub-directory (or a directory inside a sub-directory) within WordPress so that it is accessible at something like example.com/cake/ or example.com/project/cake/ in the browser.

To achieve this, you need to modify the .htaccess files used by WordPress as well as CakePHP. I’ll explain how to modify .htaccess files in the following steps.

Step 1: Modifying .htaccess file in WordPress

In the directory where WordPress is installed ( in the same folder as wp-config.php file ), edit the .htaccess file and add the following line, assuming that your CakePHP subdirectory will be called ‘cake’

RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA]

so that your .htaccess file looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA]
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
</IfModule>
# END WordPress

If your ‘cake’ is going to be inside another sub-directory of WordPress installation called ‘project’, then this line should look like:

RewriteRule ^cake/(.*)$ /project/cake/$1 [L,QSA]

If you don’t do this step, visiting example.com/cake/ or example.com/project/cake/ will try to open as a WordPress page which, obviously, isn’t right and will fail.

Step 2: Modifying .htaccess files in CakePHP

After completing Step 1, you will need to add the following RewriteBase statement in the .htaccess files used by CakePHP. If you don’t complete this step, your CakePHP app will show up but fail to load CSS / JS or any other assets in /app/webroot.

RewriteBase /cake

So, first of all, navigate to /cake/ or /project/cake/ , whichever is your CakePHP folder and edit the .htaccess so that it looks like this:

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

Then navigate to the ‘app’ folder inside ‘cake’ and edit the .htaccess file so that it looks like this:

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

Finally, navigate to webroot folder inside this ‘app’ folder so and edit the .htaccess file so that it looks like this:

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

That’s it. You’re good to go.

Visiting example.com in the browser will open your WordPress website, while example.com/cake/ or example.com/project/cake/ , depending on how you configured it, will open your CakePHP app.

11 thoughts on “Install CakePHP in a subdirectory of WordPress

  1. Thank you SO much for posting this! I was fighting with this exact setup, and was about to tear my hair out. Your solution fixed it quickly and easily. (Once I noticed Step 2. Doh.)

  2. It really great helpful….i tried to before this code whole day but it was wasted….once i got this site..i did this withing 5 Minutes….
    Simply Perfect Code….

    Thanks Abhinav

  3. Thanks for your post.

    It’s really help me & saved lot time.

    Keep Going and help us by posting above kind of articles.

  4. Hi i have used yr code but it is not working in my case i have below section

    In Word press .htaccess i used below code

    # BEGIN WordPress
    RewriteEngine On
    RewriteBase /
    RewriteRule ^filetransfertool/(.*)$ /filetransfertool/$1 [L,QSA]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress

    Here is cake root .htaccess


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

    app .htacess


    RewriteEngine on
    RewriteBase /filetransfertool
    RewriteRule ^$ webroot/ [L]
    RewriteRule (.*) webroot/$1 [L]

    webroot .htacess


    RewriteEngine On
    RewriteBase /filetransfertool
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]

    but it still shows “Page Not Found”

  5. Better than editing the 3 htscecas files is to just edit the last and set rewritebase to /and then set the root of the domain to the webroot dir of cake from within the admin that 1and1 providesif the domain is not a subdirectory for many this is a better solution with less painalso its a performance boost (perhaps the http conf can have the rewrite set in it)I just found out that the file defining vhosts is editable but have not tried this as it may beagainst 1and1 policy

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.