We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

default route '/' in micro application never found

Hi

I'm having a problem with the default '/' route in a micro application not found.

I have setup this folder structure;

webroot/
    admin/
    app/
    public/

Under webroot I have this htaccess file;

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

under both public and admin I have htaccess;

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]

and under both public and admin I have index.php files containing;

$app = new Phalcon\Mvc\Micro($di);

$app->get('/', function() {
    echo 'Public Hello';    // (echo 'Admin Hello' in admin)
});

$app->get('/info', function() {
    echo 'Public Info'; // (echo 'Admin Info' in admin)
});

$app->handle();

If I browse to <host>/admin, <host>/admin/info, or <host>/info I get the relevant response. However, browsing to <host>/ I get Not Found, when I am expecting 'Public Hello'.

If I browse to <host>/public/ I get the default response, so could it be my rewrite rules in webroot/.htaccess aren't passing the request correctly into the public folder? I've been trying without success to change these, so if anyone can see what I'm doing wrong that would be great.

thanks, Gavin



34.6k
Accepted
answer

Sometimes apache does not send _GET['url'] when it's empty. Maybe adding something like this will fix the problem.

<?php

if (!isset($_GET['url'])) {
   $_GET['url'] = '/';
}


2.0k

Interesting - I tried your suggestion and it still didn't work, so I dumped $_GET out in my public/index.php to see what was being sent in.

Now since I copied my layout from an example (can't remember where), in the webroot it also had an index.html file like so;

<html><body><h1>Mod-Rewrite is not enabled</h1><p>Please enable rewrite module on your web server to continue</body></html>

What my dump showed me was that when browsing to <host>/, my public/index.php was being given /index.html as the url, and was therefore unable to match and so was correctly saying Not Found.

By deleting this html file from my webroot, (and removing your now uneeded code fragment), <host>/ now works as it should...

I'm not entirely sure why or how apache placed index.html onto the rewrite to public/, I can't see how it would do that from the htaccess rules I have.

However my problem is solved, and I'm accepting your answer, as even though I don't need the code you suggested, you made me think!

thank you