Running in a sub-directory

I know that this question has been asked a bunch. But I still cannot get this working. I am trying to run my app from inside /api on my server.

I have read @akrabat’s blog post article and set up my htaccess file. This htaccess file exists inside of the /api folder on the server.

RewriteEngine On
RewriteBase /api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

I am using @odan’s BasePathMiddleware class. However, I have tried to also use setBasePath and it still does not work.

If I move the app to the root of the domain, everything works. When I try to use /api I get 404 on all of my routes.

I haven’t done this in years, so I have little input that I can give unfortunately.

Can you turn up Apache’s logging, particularly of the rewrite module and see anything interesting there.

When you say that you get a 404, are you getting a Slim 404 page or an Apache 404 page? If it’s an Apache one, then it’s a Rewrite issue. If it’s a Slim one, then it’s related to matching the URL.

The Slim framework is generating 404 errors, rather than Apache. So something is going on inside the app.

I just tested it out.

Folder structure for your project is

api/index.php << front controller

RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

RewriteEngine On
RewriteBase /root_dir/api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Remember to set your basepath within bootstrap file


Thank you for the help. I have read pretty much the same thing on many posts, and it finally clicked what I am doing differently! If I place my entire slimphp app inside of a subdirectory, it works perfectly. How flipping obvious is that… :man_facepalming:

What I was trying to do was to have the slim app outside my docroot and then have an /api/index.php that simply required the app that lived outside docroot. Does that make sense? I created a repo to help see what I was trying to accomplish. Let me know if this is possible.

In the Slim skeleton, the ‘public’ directory is already defined as ‘docroot’ in order to have the Slim app ‘outside’ the Slim app. Just make sure that your Apache web server document-root points to the ‘public/’ directory and that’s it.

My app is something that customers will integrate with their existing websites. This is why I thought it would be nice to allow them to install the app outside docroot then simply require the bootstrap file where they wanted the entrypoint for the app to be.

Installing the entire app within docroot does work but it would be nice if I could figure out how to do it the other way.

P.S. This is the Total CMS project that you helped me get started. I am shipping beta 1 next week.

I am fine with not storing the slim app outside of docroot. Everything works perfectly with Apache. However, I cannot get it to work in a sub-directory with the PHP built-in server.

@odan Should your Slim Skeleton repo work with the PHP server in a built-in directory?

It also works with the PHP built-in server. The command “composer start” was added for this use case.

Thanks @odan. I was able to get everything working. I ended up creating my own custom middleware that allowed me to properly route things. The problem is that in the particular
environment that I am building this to support, I do not have control over the PHP server that is running. It’s a total hack but it’s only used while in development mode and it seems to be working well.

1 Like