Heroku silex 路由显示 404 除了“/”

Heroku silex route show 404 except "/"

这段代码来自 heroku 的一个例子。但是除了路由/,我添加的任何其他内容都不起作用。它显示 404:

The requested URL /e was not found on this server.

$app->match('/', function(Symfony\Component\HttpFoundation\Request $request) use ($app) {
    return $app['twig']->render('index.twig');
});

$app->match("/dump", function(Symfony\Component\HttpFoundation\Request $request) use ($app) {
    return new Response('Thank you for your feedback!', 201);
})->bind("dump");
$app->get("/t", function() use ($app) {
    return new Response('Thank you for your feedback!', 201);
})->bind("t");
$app->get("/d", function() {
    return new Response('Thank you for your feedback!', 201);
})->bind("d");
$app->get("/e", function() {
    return new Response('Thank you for your feedback!', 201);
});
$app->run();

编辑 1 我直接将它部署在 heroku 服务器上(heroku 在每次推送到 master 分支时自动构建)

编辑 2 我的工作区:

bin\worker.php
www\index.php <== the snippet is from this file
www\list.php
apache_app.conf
app.php  <== basic init, return Silex\Application $app
Procfile

apache_app.conf的内容复制自this link。 重写引擎开启

RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteRule ^(.*) - [E=BASE:%1]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

RewriteRule .? %{ENV:BASE}/app.php [L]

我想我需要以某种方式更改 apache 配置,但我不明白 htaccess 语法。

对于 apache2,您可以在“/web”中添加一个 .htaccess 文件

<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On
    #RewriteBase /path/to/app
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
</IfModule>

并将您的 Procfile 设置为:

web: vendor/bin/heroku-php-apache2 web/

对于 nginx,创建一个包含以下内容的 nginx conf 文件:

location / {
    # try to serve file directly, fallback to rewrite
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    # rewrite all to index.php
    rewrite ^(.*)$ /index.php/ last;
}

location ~ ^/(index|index_dev)\.php(/|$) {
    try_files @heroku-fcgi @heroku-fcgi;
    internal;
}

并将 Procfile 设置为:

web: vendor/bin/heroku-php-nginx -C server_conf/nginx.conf web/ 

我遇到了同样的问题,它困扰了我几个星期……以至于我刚开始使用 / 作为我唯一的路由并通过传入变量添加其他功能。把我逼上墙

我终于真正需要修复它了,几个小时后发现了这个 post:

https://laracasts.com/discuss/channels/laravel/laravel-v5415-on-heroku-all-routes-but-not-working

TL/DR;我复制了一个(正常工作的)heroku 存储库来创建我的新存储库,并且在某个地方我的 web/.htaccess 文件被删除了。我更换了它,一切都很顺利。

对我来说,提供的测试也很有效(我正在使用 PHP):如果 eaxmple.com/ROUTE 不起作用,但是 example.com/index.php/ROUTE 有用吗……你有一个 .htaccess 问题。

此外,作为参考,这是我的整个 .htaccess 文件:

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

希望这可以节省一些搜索...

很简单,安装apache就可以了:

composer require apache-pack