Install CakePHP 3.x in Sub Directory htaccess mod_rewrite Rules

When you install a CakePHP app you need to tell your server to point to the webroot directory of the project. The most common way to do this on an apache server is to modify the 000-default.confย file accordingly. This is explain as part of this tutorial on how to setup a CakePHP app on Ubuntu 16.06.

Sometimes, particularly during development, you may want to have a Cake project installed in a sub directory on your server. I recently had a need for this where I had a WordPress install on the public root directory, and wanted a Cake project in a sub directory. One way to do this is to create a sub domain. Another (arguably more convenient) way of doing it is to control urls in the htaccess file.

You’ll of course need the mod_rewite module enabled on the server, which will be a requirement for CakePHP anyway.

The setup here is that I have a CakePHP app installed to the demo directory, obviously change “demo” to match your project name.

Firstly modify the htaccess file in the root directory. In this example I’ll give the whole file, most of which comes from WordPress, only one line is added:

The Root htaccess File

The addition to this file is:

The htaccess file in your CakePHP Directory

Now head into the CakePHP application directory (so in this example the demo directory). You’ll need to update the htaccess file in here as well:

You should now be able to go to http://your_domain/demo and be presented with your Cake application.