Rodolpho Gheleri Rodolpho Gheleri - 1 month ago 15
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.

http://pastebin.com/2mgSjWWV

http://pastebin.com/yD4RpfK8

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

Answer

This:

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 example.com/foo 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 example.com/foo would be identical to a request for example.com/index.php. 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:

example.com/foo      -> example.com/index.php?args=foo
example.com/bar?baz  -> example.com/index.php?args=bar&baz
Comments