1

My problem is I can't add a route other than '/'. if I change / to /hello I get a 404 error. I think I have a mistake in my paths or .htaccess.

my .htaccess:

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

here is my code and my project structure

require '../../vendor/slim/slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
require_once '../../vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/hello', function () {   //'/' works fine
    echo "Hello";
});
$app->run();

enter image description here

alexw
  • 8,468
  • 6
  • 54
  • 86
  • Are you going to the right URL? So `localhost/hello` ? Your code is fine. Ofcourse will the `/` respond with a `404` because you have changed it to `/hello` – GuyT Jun 30 '15 at 11:00
  • I go to : http://localhost/API/v1/DEVOLO_UI/form/hello –  Jun 30 '15 at 11:34
  • To which folder is your documentroot pointed? I guess you need to use `localhost/DEVOLO_UI/hello` and what shows your developer tools(network tab)? – GuyT Jun 30 '15 at 11:38
  • the vendor folder is in localhost/API/v1 but my index.php is in localhost/API/v1/DEVOLO_UI/form –  Jun 30 '15 at 11:42
  • What does yout htaccess say? Looks like there is the problem. Your htacces has to forward all requests to that index.php. – danopz Jul 02 '15 at 06:35
  • Btw / works because you are requesting index.php - /hello requests file/folder named hello. – danopz Jul 02 '15 at 06:38
  • @bourax Can you add the content of your .htaccess to your question please? And can you try to move your `index.php` file to `v1` directory? – Davide Pastore Jul 02 '15 at 21:16

1 Answers1

1

In your .htaccess file you have the following rule:

RewriteRule ^ index.php [QSA,L] –

Since you're not specifying a path for index.php, Apache will try to load an index.php file in the current directory. But since that file is not there you'll get a response with error 404.

But since the .htaccess file is not under the directory you are accessing, it's not even loaded by the server. You need to do one of the following:

  1. Move index.php file to project root dir, as people suggested in the comments on your question (which is the best solution).
  2. Move .htaccess to the same directory as index.php (it seems to be DEVOLO_UI/form).

By the way, have you considered use only Composer's autoload? You don't need to call both autoloads: Slim's and Composer's. In your index.php you may write something like this:

// Set the current dir to application's root.
// You may have to change the path depending on 
// where you'll keep your index.php.
$path = realpath('../../');
chdir($path);

require 'vendor/autoload.php';

$app = new \Slim\Slim();

// Your routes followed by $app->run();
// ...
Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62