The correct way to set CakePHP base URL

ondra.cifka picture ondra.cifka · Aug 12, 2014 · Viewed 8.3k times · Source

My CakePHP site resides at http://localhost/somepath/. How should I set up CakePHP so that everything works?

I tried adding add the line

RewriteBase  /somepath/

to .htaccess and app/webroot/.htaccess. There's no other way I can make URL rewriting work (otherwise, I always get a 404 when accessing the site). This solution works, but breaks routing when used from a CLI. For example, when I use

Router::url(array('controller' => 'posts', 'action' => 'view', 3), true);

in a Shell, I get http://localhost/posts/view/3 instead of http://localhost/somepath/posts/view/3.

So in addition to changing .htaccess, I also tried setting App.fullBaseUrl in core.php, like this:

Configure::write('App.fullBaseUrl', 'http://localhost/somepath');

This way, I get correct URLs in Shells, but not on web pages ­– e.g. using the same Router::url call as above, I get http://localhost/somepath/somepath/posts/view/3.

Answer

Oops D'oh picture Oops D'oh · Sep 1, 2014

Should the /somepath/ be visible in the url or do you try to hide this subfolder in the url?

Have you tried to add RewriteBase /somepath/ in all 3 htaccess files?

/yourcakedir/.htaccess
/yourcakedir/app/.htaccess
/yourcakedir/app/webroot/.htaccess

and then in

/yourcakedir/app/config/bootstrap.php

add

Configure::write('App.base','');

You can also add

Configure::write('App.base','/');

but this can bring up double slashes in some cases, because your htaccess RewriteBase already ends with a slash.

If you set the App.base you shouldn't need this line in your core.php:

Configure::write('App.fullBaseUrl', 'http://localhost/somepath');