Redirect or URL builder in CakePHP "expanding" again htaccess redirect

Hi everyone,

I have installed recent CakePHP into a subfolder of my webspace, let’s say
/dir1/dir2/dir3/myapp/
I have setup htaccess to directly serve this folder, when accessing it from a specifc domain, let’s say

RewriteCond %{HTTP_HOST} ^(www.|)domain.io$
RewriteRule ^(.*)$ /dir1/dir2/dir3/myapp/$1 [L]

This worked as expected. I was able to directly call controllers and it would show properly in my browser’s URL, like so
domain.io/MyController/some-method

However, any CakePHP generated redirect, or URL builder, would again include my original subdirectories in the URL, nullifying the expected .htaccess redirect, like so
domain.io/dir1/dir2/dir3/myapp/MyController/some-method

Even though this is technically working, i do not want to show the subdirectories.
What is affecting this behaviour and how can i make CakePHP redirect or build URLs on my shortened URL?

Couldn’t you use Cake’s routing instead of the .htaccess?

I use .htaccess for security only, like protecting folders from being explored, and redirecting http to https, but I wouldn’t use it for website navigation - for me that’s the wrong tool for the job, that’s what the routing is for.

1 Like

Well, not sure if I understand. I’m using Cake PHP for any application routing. But how would I be able to rewrite an URL of a subdirectory to be served in root of a domain?

Intermediate status:
After some debugging i found how the URL was built:

We’re building the URL based on PHP_SELF - which unfortunately has the full path (/dir1/dir2/dir3/myapp/MyController) not the redirect path (/MyController).

Still investigating, how to change that. When directly calling the redirect URL, it’s actually working. I just want the URLs of cakephp being built in that same redirected way. :slight_smile:

Ok ok, i’m still learning (and reading) - so i missed i could set “App.base” manually in the config. This seems to be the way i should be going - right?

But: theoretically this would have to be just ‘/’ as cakephp is virtually sitting in root, after the redirect. Unfortunately “App.base” needs to be a directory and cannot be just ‘/’ :frowning:

Just set App.base to an empty string and you should be good to go.

3 Likes

Oh man. Thank you! That works.

I was so focused on the explanation in the cookbook, that I have to ensure it starts with a “/”, so i didn’t even think of leaving it empty. :frowning:

App.base
The base directory the app resides in. If false this will be auto detected. If not false, ensure your string starts with a / and does NOT end with a /. E.g., /basedir is a valid App.base. Otherwise, the AuthComponent will not work properly.