Page Not Found - 404

I am trying to right a basic controller that all requests get redirected through. I have made a simple Slim app that runs in this controller, and I keep getting the dreaded slim ‘Page Not Found’ page. Have gone through a ton of Google searches and none of the answers fix my problem. Finally decided to post in here.

This is my basic Apache Conf, and mod rewrite is on.

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^.* /controller.php [QSA,L]

So everything should be redirected through the controller script in the web root directory.

controller.php looks like this:
<?php
require_once( DIR . ‘/config.php’ );
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

 $config = [ 
        'settings' => [
        'displayErrorDetails' => true,
        'addContentLengthHeader' => false,
      ],  
 ];
 $app = new \Slim\App($config);
 $app->get('/test/', function ($request, $resp, $args) {
    return $resp->withStatus(200)->write(print_r($resp, true));

 });
 $app->run();

I know this script is getting called because I can do a “print_r($_SERVER); exit;” and see the output. But for some reason my route is not being registered.

What am I doing wrong?

And you’ve tried using the following like the website suggests?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . controller.php [L]

and then
require_once( DIR . ‘/config.php’ );

I see the two underscores next to DIR aren’t displayed here for some reason, but I assume you have them there

Also, remove the last forwardslash in your route so that it reads

$app->get(’/test’ … instead of $app->get(’/test/’ …

Yes, the last slash I have tried to remove as well. Does not work. The page loads fine, there are no syntax errors. The two underscores don’t show here, but they are there. _ _ DIR _ _.

The rewrite rules you have shown are what I have.

The only thing I can think of is that I am using this as a virtual host. Would that have any impact?

Sorry, friend, i’m not too sure about that. Hopefully someone else can assist further

Actually I just figured it out. I didn’t have the .htaccess file in the document root directory.

Also, it should look like this:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^ controller.php [QSA,L]

Champion! Well done!

Actually I spoke too soon. This does not work. Something is wrong with how Slim is trying to match the route paths.

In the $_SERVER var, I have these. Which one does Slim use to match against paths?

[SCRIPT_URL] => /test
[SCRIPT_URI] => https://myserver.com/test

So I’m accessing: myserver.com/test

I added these lines right before $app->run():

print "URI: " . $app->getContainer()->get(‘request’)->getUri();
print "PATH: " . $app->getContainer()->get(‘request’)->getUri()->getPath();

This is the output:
URI: https://myserver.com/test/
PATH: /

So why would the URI have the trailing forward slash? Also, when getting the path, why would it NOT include the first path dir ‘test’?