When developing with CakePHP, we can setup Apache to automatically redirect a domain to a specific CakePHP application.

We can set computer’s aliases using hosts file. Apache will automatically map aliases to different DocumentRoot:

app1.mybox.com -> /var/www/cakephp/app1.mybox.com/webroot
app2.somewhere.com -> /var/www/cakephp/app2.somewhere.com/webroot

Step 1: Enable the mod_vhost_alias:

$ sudo a2enmod vhost_alias

Step 2: Replace the DocumentRoot statement in default virtualhost configuration file with two lines:

UseCanonicalName    Off
VirtualDocumentRoot /var/www/cakephp/%0/webroot

(If you use a different virtualhost, make sure that you’re editing the right configuration file)

Step 3: However, the mod_rewrite doesn’t play nice with mod_vhost_alias. We need to edit the .htaccess file in {APP}/webroot folder.

    RewriteEngine On
    RewriteBase /   # << Add this line
    RewriteCond %{REQUEST_FILENAME} !-d

Step 4: Restart Apache.

sudo service apache2 restart

Below is my default virtual host configuration for your reference:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

        UseCanonicalName Off
    VirtualDocumentRoot /var/www/cakephp/%0/webroot

    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
</VirtualHost>