PHP Slim Framework: The requested URL was not found on this server

CodeMonkey picture CodeMonkey · Apr 1, 2015 · Viewed 12.3k times · Source

For one of the projects, i am using Slim Framework http://www.slimframework.com/ to create restful APIs in PHP.

I did a manual install for the framework by copying it in the PHP project folder using instructions at https://github.com/slimphp/Slim.

Later i updated my .htaccess as well.

For my project, I have the following Directory Structure

project\
----slim\
----tests\
----index.php
----.htaccess

For this, the Get call i.e. http://someIp/project/ works for me. It fetches the standard "Welcome to Slim! Congratulations! Your Slim application is running. If this is your first time using Slim, start with this "Hello World" Tutorial." However, post/patch/delete and other get are not working. Not even get for hello. It gives the not found error.

http://someIp/project/hello/:name The requested URL /project/hello/:name was not found on this server.

http://someIp/project/post The requested URL /project/post was not found on this server.

Updated my .htaccess file as :

RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Still it is failing.

When i made changes into the apache config file to allowOverride = all, it failed even for the GET call on index.php. For sure, it is not mapping from .htaccess.

I am still clueless what changes i need to make to .htaccess or any other file to get it working.

Here is the code:

\Slim\Slim::registerAutoloader();

/**
 * Step 2: Instantiate a Slim application
 *
 * This example instantiates a Slim application using
 * its default settings. However, you will usually configure
 * your Slim application now by passing an associative array
 * of setting names and values into the application constructor.
 */
$app = new \Slim\Slim();

/**
 * Step 3: Define the Slim application routes
 *
 * Here we define several Slim application routes that respond
 * to appropriate HTTP request methods. In this example, the second
 * argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete`
 * is an anonymous function.
 */

// GET route
$app->get(
    '/',
    function () {
        $template = "hi";
        echo $template;
    }
);

//$app->get(
//    '/v1/status/',
//    function() {
//        echo "status";
//    }
//);
//
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});

// POST route
$app->post(
    '/post',
    function () {
        echo 'This is a POST route';
    }
);

// PUT route
$app->put(
    '/put',
    function () {
        echo 'This is a PUT route';
    }
);

// PATCH route
$app->patch('/patch', function () {
    echo 'This is a PATCH route';
});

// DELETE route
$app->delete(
    '/delete',
    function () {
        echo 'This is a DELETE route';
    }
);

/**
 * Step 4: Run the Slim application
 *
 * This method should be called last. This executes the Slim application
 * and returns the HTTP response to the HTTP client.
 */
$app->run();

Answer

CodeMonkey picture CodeMonkey · Apr 5, 2015

The following steps worked for me :

Changes in apache2.conf

1. Get the path of running Apache
    ps -ef | grep apache
   Append -V argument to the path
    /usr/sbin/apache2 -V | grep SERVER_CONFIG_FILE

2. Naviagte to apache2.conf
    vi /etc/apache2/apache2.conf

3. Update the file Replace "AllowOverride None" to "AllowOverride All"
    <Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>
4. Restart apache2 after
    service apache2 restart
   OR
    apachectl -k graceful

Changes in .htaccess

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Changes in .htaccess inside subdirectory

RewriteEngine On
RewriteBase /project
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Enable mod_rewrite for Apache 2.2

1. Type the following command in the terminal
    a2enmod rewrite
2. Restart apache2 after
    service apache2 restart