Rodolpho Gheleri Rodolpho Gheleri - 1 year ago 69
PHP Question

Apache2 not working with routes PHP

I am developing an app with PHP 7.0 and implementing routes with MVC. My root folder ('/') is the 'public' directory. When I access the address 'localhost' I am redirected to index.php with have the routes available. But when I try another url to access another route, like 'localhost/contact' the server doesn't find the entry and give this message:

Not Found

The requested URL /contact was not found on this server.

I am pretty sure that the problem in on my server config (apache2 on linux mint 18), because my friend's PC works normal. I'm using a .htaccess file too inside the public directory:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

It seems to me that the server does not run index.php and tries to access the path. How could I ever force the execution of index.php to see if there is a route to the url informed?

here follows my apache2 config files.

I'm still newbee and I understand very little in server configuration. Please someone can give me a light? Thanks!

Answer Source


RewriteRule ^.*$ - [NC,L]

It matches EVERYTHING, and since it's tagged [L], no other RewriteRules will be evaluated, so all rewriting stops here. That means a request for will match this rule, rewriting stops, and a literal foo file will be searched for in the file system - which doesn't exist.

And then, even if this one rule wasn't there, this next line

RewriteRule ^.*$ index.php [NC,L]

would also not work. ANY url would match it, but then strip off the relevant data, so a request for would be identical to a request for no query parameters would be passed in.

Your logic should be more like:

RewriteRule ^(.*)$ index.php?args=$1  [QSA,L]

which would do these sorts of translations:      ->  ->
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download