Slim Framework always return 404 Error

Slim Framework always return 404 Error

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Slim Framework Always Return 404 Error

Encountering a persistent 404 Not Found error immediately after calling $app->run() in the Slim Framework is one of the most common initial hurdles when deploying a PHP API on an Apache server. This issue usually stems from a misconfiguration in how the web server handles requests destined for your application's bootstrap file, rather than an error within Slim itself.

When you instantiate Slim and define routes like $app->get('/books', ...)$, the framework relies entirely on the HTTP request reaching the bootstrap.php (or whatever entry point you configure) file. If Apache serves a different file or cannot locate the rewrite rules, the browser receives a 404 response before your application logic ever executes.

The Rewrite Engine Requirement

The core of the problem lies in the .htaccess configuration. Slim requires Apache to intercept every request and route it to the main entry point, regardless of the URL path requested. Your current configuration shows:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ bootstrap.php [QSA,L]

This logic is sound, but there are two critical prerequisites that often get overlooked in local development environments. First, mod_rewrite must be explicitly enabled in your Apache configuration (httpd.conf or included via LoadModule rewrite_module modules/mod_rewrite.so). If this module is disabled, Apache ignores all Rewrite directives, serving the default file found in your document root (often index.php or index.html) instead of forwarding the request to bootstrap.php.

Second, and more subtly, the path to bootstrap.php must match exactly what the server expects. In many local setups using XAMPP or WAMP, the default index file is index.php. If your application root contains an index.php that does not invoke Slim, Apache will serve that file first, resulting in a 404 if Slim's entry logic isn't properly triggered.

Verifying the Document Root

A frequent pitfall occurs when the web server's DocumentRoot points to the wrong directory or when the bootstrap.php file is not directly accessible within that root. Ensure that your httpd.conf (or virtual host configuration) explicitly sets the DirectoryIndex directive to point to bootstrap.php if you do not intend to use a default index file:

<Directory /path/to/your/slim/app>
    Options +FollowSymLinks
    AllowOverride All
</Directory>

# Explicitly setting bootstrap.php as the entry point helps clarity
DirectoryIndex bootstrap.php

However, relying solely on DirectoryIndex is often insufficient. The .htaccess file must successfully intercept all non-file requests and redirect them to bootstrap.php. If you see a 404, check the browser's developer tools (Network tab) to see the actual response headers. Sometimes, the server might be returning a 404 because the bootstrap.php file itself is missing from the expected path, or the PHP handler is failing silently before Slim can generate a response.

Testing Without Apache Overhead

To isolate whether this is an Apache-specific routing issue or a code error, try running Slim using PHP's built-in server instead of Apache temporarily:

php -S localhost:8000 -t public_dir
# Or if bootstrap.php is in the root:
php -S localhost:8000 bootstrap.php

Note: The -t flag points to the document root. If you use this method, ensure your code expects $_SERVER['REQUEST_URI'] to be passed correctly.

If the application runs perfectly on the CLI server but fails with a 404 on Apache, the issue is strictly within the Apache configuration or module loading. Check the Apache error logs (/var/log/apache2/error.log or equivalent) for messages regarding mod_rewrite being disabled or permission denials.

Modernizing the Setup

While the manual steps described in older tutorials (like those from Coenraets or CodingThis referenced in similar discussions) are foundational, modern PHP distributions often bundle Slim differently. Many developers now prefer using Composer to manage dependencies and avoid manually placing files into specific directories.

Using Composer ensures that Slim/Slim.php is located correctly relative to your project structure, preventing "File not found" errors that can manifest as 404s if the autoloader isn't initialized in bootstrap.php. The standard modern approach looks like this:

// bootstrap.php
require 'vendor/autoload.php';
$app = new \Slim\Slim();

$app->get('/books/:id', function ($id) {
    echo "Book ID: {$id}";
});

$app->run();

If you are maintaining an older codebase based on the source material, ensure that require('Slim/Slim.php'); is actually pointing to the downloaded file and that no other framework (like Laravel) is interfering with the routing stack. If you find yourself needing a more robust ecosystem for your PHP API later, frameworks like Laravel offer built-in routing and middleware that abstract away much of this manual configuration complexity, though Slim remains a lightweight choice for simple REST services.

By ensuring mod_rewrite is active, verifying the path to bootstrap.php, and checking the server logs for silent failures, you can resolve the 404 error and get your Slim API serving requests correctly.

Our network