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

问题描述 投票:2回答:2

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

在此服务器上找不到请求的URL / e。

$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自动构建)

编辑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复制的。 RewriteEngine On

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

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

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

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

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

php heroku silex
2个回答
1
投票

对于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/$1 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/ 

0
投票

我有同样的问题,它让我困扰了好几个星期......我刚刚开始使用/作为我唯一的路线,并通过传入的变量添加其他功能。把我赶到了墙上。

我终于有必要真正解决它,几个小时后发现这篇文章:

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

TL / DR;我已经复制了一个(正确工作)heroku repo来创建我的新版本,并且在我的web/.htaccess文件被删除的某个地方。我取而代之,一切都像魅力一样。

对我来说,提供的测试也运行良好(我使用PHP):如果eaxmple.com/ROUTE不起作用,但example.com/index.php/ROUTE确实有效...你有一个.htaccess问题。

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.