PHP (Slim) routers don't work after hosting in subdirectory (Using Apache and Ubuntu 16)

Posted August 16, 2018 3.1k views

I have a few different projects and all of them are set in /var/www/html folder. Each one has its own folder. That’s what I wrote in my virtual host to activate all of them:

<VirtualHost *:80>
    DocumentRoot /var/www/html/

One of my projects has routes in it - it’s written in PHP using Slim micro framework.

The project is in var/www/html/school folder.

This is my .htaccess file:

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

All of the routers are at routers.php that is required in index.php and they don’t work: could you help me?

That’s the website: The login page is the only one that works - I’ve tried adding the word ’/school’ to every router in my routers.php file but this does'nt work.

I know that if I change my DocumentRoot to /var/www/html/school the project will work perfectly but then all other projects will stop working.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Seems like you need to provide the correct path to routers.php in index.php.

  • Could you elaborate?
    But the project works when I change my DocumentRoot on Apache. Shouldn’t I change my .htaccess file?

    • Let’s say that in index.php the path now is /stuff.php or something like rootdir/stuff.php and since you are using a subdirectory the path should include the subdirectory. So the new path would be something like school/stuff.php or rootdir/school/stuff.php . This is why changing the root dir in apache will make it work i guess.