After setting up a fresh copy of CakePHP project, the css reference from the code is:


While the real location of my css files is:


Why is that?

Question author Imri-persiado | Source



Why is that?

CakePHP, as with most frameworks, makes use of url rewriting by default (not a requirement, it can also be turned off). For apache users this is implemented via .htaccess files, such as this one in the root of your application:

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

And another in the webroot - the presence of these files mean url rewriting just-works for the majority of users.

The rules in the application root folder rewrite all requests to the webroot folder, thus these urls are actually equivalent:


The primary purpose of these rewrite rules is security - it prevents the possibility of accessing application files outside the webroot folder - something which is not necessary if using a production install. A side effect of this is that urls remain the same whether it's a development-style or production-style install.

Use helper classes

Classes such as the html helper take care of knowing where files are and linking to them appropriately, don't be tempted to hardcode the path to your assets in your template files i.e. don't do this:

    <link rel="stylesheet" href="/my_app_name/css/cake.css" />

instead do this:

    <?= $this->Html->css('forms'); ?>

Otherwise, you'll find that even something as simple as renaming your application mean links/assets break.

Answer author Ad7six

Ask about this question here!