AltoRouter not doing the routing correctly

2019-08-25 05:57发布

I'm just developing a little WebApp with PHP and for routing, I use AltoRouter.

So far, I was only developing and testing offline, so on my localhost. Now, to see the behaviour on a server, I uploaded my code, fixed the database connection to fit for the MySQL-Server running on the server (which is Ubuntu 14.04) and tried to get it started.

First, what I'm doing for the routing, is a simple .htaccess, looking like that:

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

And then, in my index.php, the routing is done

<?php 

include 'vendor/altorouter/altorouter/AltoRouter.php';

$router = new AltoRouter();
$router->setBasePath('');

$router->map('GET','/','home.php','home');
$router->map('GET','/datenschutz','datenschutz.php','datenschutz');
$router->map('GET','/impressum','impressum.php','impressum');

$router->map('GET','/header','header.php','header');
$router->map('GET','/jquery','vendor/components/jquery/jquery.js','jquery');
$router->map('GET','/bootstrapjs','vendor/twbs/bootstrap/dist/js/bootstrap.js','bootstrapjs');
$router->map('GET','/bootstrapcss','vendor/twbs/bootstrap/dist/css/bootstrap.css','bootstrapcss');
$router->map('GET','/generalStyle','style.css','generalStyle');
$match = $router->match();
if($match) {
  if($match['name'] === 'generalStyle'){
    header("Content-Type: text/css");
    $fileName = $match['target'];
    echo file_get_contents($fileName);
    return;
  }
  require $match['target'];
}
else {
  header("HTTP/1.0 404 Not Found");
  require '404.php';
}

?>

This is perfectly working on my localhost, so there doesn't appear to be a problem in general.

However, on my server, when heading forward to the IP, it first seems, that it's working, because when only going to /, the home.php file is loaded correctly. But, whatever other route I call, like /impressum, it's not working, going back to /, the home.php is working again.

Now, replacing home.php (in the route) with impressum.php is working as well, then I can see the correct file as well. So to summarize, it's seems like it's always only the first route that is working.

Does anybody have an idea, why this happens or what the problem is?

I tried to call the files directly, and (besides some scripts not loaded etc.) its working, so going to myDomain.com/impressum.php is working fine, so it doesn't seem to be a problem of the file or sth.

I also tried to set the permission of the hole project to 755, to be sure it's not a problem with that, same results though.

Any ideas?

EDIT: I've been trying around a lot, but still didn't got it working.... However, I figured, that it's not "the first route" working, but only the /-route, which is working. Every other route isn't working, but again, everything is working on localhost with XAMPP running...

1条回答
等我变得足够好
2楼-- · 2019-08-25 06:28

Finally, I solved the problem..

It all wasn't about the PHP, AltoRouter, htaccess or whatever...

The solution was to set AllowOverride to All in the Directory of my vhost-config....

查看更多
登录 后发表回答