Defining Document Root in Apache2/Cake PhP

apache, cakephp, mod-rewrite, php

Solution

Since you have access to edit the apache config files - it's most appropriate to make a standard production install.

Fixing the docroot

does the above config block need to have: .../app/webroot as the DocumentRoot

Assuming that path exists: yes.

This will fix the broken css/js/images.

Fixing mod rewrite

Put the rewrite rules in the virtual host config:

DocumentRoot /u02/data/docroots/this_site/app/webroot
<Directory /u02/data/docroots/this_site/app/webroot>
        Options -Indexes
        AllowOverride none
        Order deny,allow
        Allow from all

        // added
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
</Directory> 

This will then route requests for anything that's not a file to the application.

Verify that the rewrite rule used is compatible with the version of CakePHP in use, the above is based on the rule for the latest release - it changed over the years and using the wrong rewrite rule may have unexpected side effects.

What's actually broken

AllowOverride none

This prevents the `.htaccess` files from being read by apache. If you were to change this to `AllowOverride all`, and assuming the currently-ignored `.htaccess` files exist, the site would work - but as a development install.

Problem

I have a broken Cake PHP site that I did not write, but am charged with fixing. I have the main controller up, however no css/js/images, and when I click a link I get a 404 not found. I believe something is incorrect with mod_rewrite or the docroot config in apache. While reading through Cake documentation, I came across this: "app/webroot In a production setup, this folder should serve as the document root for your application. Folders here also serve as holding places for CSS stylesheets, images, and JavaScript files." In my /etc/apache2/sites-enabled/this-site, I see this: ``` DocumentRoot /u02/data/docroots/this_site <Directory /u02/data/docroots/this_site> Options -Indexes AllowOverride none Order deny,allow Allow from all </Directory> ``` So, my question: does the above config block need to have: /u02/data/docroots/this_site/app/webroot as the DocumentRoot and ? Anywhere else you can think to look for troubleshooting this?

Original source