Slim 4 - HttpNotFoundException

Here is how I made it sing:

  • root folder in apache24/htdocs/ accessible as localhost/

  • slim in localhost/vendor

  • project in localhost/

  • .htaccess in root folder (localhost/)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
  • index.php in root folder
<?php
use Slim\Factory\AppFactory;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

require __DIR__ . '/vendor/autoload.php'; 

$app = AppFactory::create();

$app->get('/', function (Request $request, Response $response, $args) {
    $response->getBody()->write('Hello world!');
    return $response;
});

$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $name=$args['name'];
    $response->getBody()->write("Hello $name!");
    return $response;
});

$app->run();

If I move .htaccess and index.php to a public folder located in my web root, then the index.php should be like this:

<?php
use Slim\Factory\AppFactory;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

require __DIR__ . '/../vendor/autoload.php';         <====== CHANGE HERE 

$app = AppFactory::create();
$app->setBasePath("/public");                        <====== CHANGE HERE 

$app->get('/', function (Request $request, Response $response, $args) {
    $response->getBody()->write('Hello world!');
    return $response;
});

$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $name=$args['name'];
    $response->getBody()->write("Hello $name!");
    return $response;
});

$app->run();

I had similar errors to those mentioned by others and the problem was that although the mod_rewrite module of Apache was enabled, it was not working because the AllowOverride setting in httpd.conf of Apache is set to None by default. You should leave this to None for the global setting (for security - as suggested), but you should change it to “AllowOverride All” for the virtual host you are working with (that is: <Directory “${SRVROOT}/htdocs”> for the default localhost).

This is stated in the documentation here:


but people just follow the basic installation guidelines of SLIM that do not mention this. It took a whole day to figure it out! Moreover, most examples use the local PHP server starting it from inside the public folder. It is different if you use Apache serving from the localhost.

The above should work with any virtual host defined in Apache.