Laravel 路由指向主页而不是抛出 NotFoundHttpException

Laravel route directing to Home instead of throwing a NotFoundHttpException

我在 laravel (4.2) 路线中遇到一些意外行为。

假设我的服务器在 https://example.com. If I enter https://example.com/whatever/index.php 可用,我希望 laravel 抛出 NotFoundHttpException,因为未定义到 "whatever" 的路由。相反 laravel 向我显示起始页,表明我的 "home" 路线已被捕获。

如果我只输入 https://example.com/whatever everything is fine (i.e. I get the NotFoundHttpException as expected). I neither have the problem on my localhost. Here https://localhost/laravel/whatever/index.php 将按预期抛出 NotFoundHttpException。

我的 routes.php 文件:

// Home
Route::get('/', array( 'as' => 'home', function() {
    return View::make('home');
}));

也许有人可以提示我从哪里开始搜索导致该行为的原因:Apache 配置、PHP 配置、Laravel 配置?

作为对 S. Safdar 的答复的修正案: 起初我也想到了 .htaccess 重定向问题。在 laravel 的 public 文件夹(Web 服务器根目录)中放置一个 .htaccess,如下所示:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ / [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

如您所见,此处的重定向处理正确。请求的 "whatever/index.php" 不是真正的文件,因此它被重定向到由 laravels index.php 处理。如果我删除重写条件(加上规则),我会得到一个常规的 apache 404 错误页面。在我的情况下,这没有帮助,因为我希望 laravel 正确(!)处理所有错误页面。但出于某种原因,laravel 的主路线匹配每个以 /index.php 结束的 url。

你的路线为什么不用这个?

// Home
Route::get('/', function() {
    return View::make('home');
}));

我认为问题出在您在路由数组中使用的 as

nginx 和 apache 服务器似乎都存在这个问题。我采取了一些步骤来缓解 apache:

的问题

更改 index.php 文件名:

public/index.php to public/start.php

将 .htaccess 更改为:

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

将 server.php 文件中对 index.php 的引用更改为:

require_once $paths['public'].'/start.php';