0 Routers don't work in my PHP / Slim project (Ubuntu, PHP, Slim, Twig, PuTTY, FileZilla, Apache)

August 12, 2018 511 views
Apache

I have been trying to host a website (php project) on Digital Ocean using Ubuntu 16.04.5 and apparently it's pretty complicated for the inexperienced. I uploaded all files to var/www/html using FileZilla and the main router doesn't work. Instead of domain/login router the only router that works is domain/index.php/login. I'll show you - http://www.itayperry.com/index.php/login

Why does this happen? The index.php should'nt be there. I would love for some help :)

BTW, the project is built in a Slim Framework (with Twig) and the main file is index.php containing a routers.php requirement indise of it.
These are my project files on GitHub:
https://github.com/itayperry/School-Project
and I'm using Apache 2.4.18

2 Answers

Ensure the .htaccess file in the directory structure contains:

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

You also need a directory directive to enable .htaccess files and allow the RewriteEngine directive to be used. This is sometimes done globally in the httpd.conf file, but its generally a good idea to limit the directive to just your virtual host by enclosing it in your VirtualHost configuration block. This is generally setup in your configuration in the form of:

<VirtualHost *:80>
    ServerAdmin me@mysite.com
    DocumentRoot "/path/www.mysite.com/public_html"
    ServerName mysite.com
    ServerAlias www.mysite.com

    #ErrorLog "logs/mysite.com-error.log"
    #CustomLog "logs/mysite.com-access.log" combined

    <Directory "/path/www.mysite.com/public_html">
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

As a result, Apache will send all requests for non-existent files to my index.php script in which I instantiate and run my Slim application. With URL rewriting enabled and assuming the following Slim application is defined in index.php, you can access the application route below at “/login” rather than “/index.php/login”.

ServerName mysite.com
ServerAlias www.mysite.com

 <Directory "/path/www.mysite.com/public_html">
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

All of this doesn't appear on my virtual host - but still - I swiped the .htaccess file with what you told me to write and then I restarted the Apache and it worked!!!!
Thank you so much!
I've been stuck for three days. I'm not a pro and these things are very complicated.

Btw, I have a database for that project in phpMyAdmin, can I connect it to the project? and will it be updated after every user interaction? Or is it better to copy it and let all the changes be done solely on the server?
I'd be happy to get your advice ;)

Have another answer? Share your knowledge.