Laravel重定向不存在路由

时间:2018-02-05 11:07:36

标签: php laravel laravel-routing

我在App/Exceptions/Handler.php方法的render()中有这段代码:

if ($this->isHttpException($e)) {
    switch ($e->getStatusCode()) {
        // not found
        case '404';
            return redirect()->route('index');
            break;

        // internal error
        case '500';
            return redirect()->route('index');
            break;

        default;
            return $this->renderHttpException($e);
            break;
    }
} else {
    return parent::render($request, $e);
}

但问题是,当我转到localhost:8000/vendor时,我没有被重定向。我收到以下回复:

  

在此服务器上找不到请求的资源/供应商。

我希望所有导致404 Not Found的网址都被重定向到我的主页。

2 个答案:

答案 0 :(得分:3)

问题是某些软件包将资产发布到public/vendor,因此它是文件夹,并且根据您的网络服务器的设置,文件夹可能 或者可用于列出(某些Web服务器为Options +Indexes)。大多数情况下,不允许列出其内容,因此您可以获得404 Not Found403 Forbidden

要在Apache中重定向,请将以下内容放在位于.htaccess的{​​{1}}文件中。

public/

如果您使用的是nginx,请在配置文件中输入:

ErrorDocument 404 https://your-website.com/

对于内置的PHP服务器(error_page 404 /; eq),你应该以某种方式手动完成...希望你只是用它来开发你的应用程序和生产,所以它不应该&#39无所谓。

答案 1 :(得分:1)

我将使用404路线处理它,在路线文件中(我想抓住我路线中不存在的所有内容。我希望将它们重定向到我的主页。)< / p>

Route::fallback(function ()
{
    # To Specific Controller
    return Redirect::to('homeController'); # ('/') if defined 

    # To Specific View
    return response()->view('custom.file.path', [], 404);
});
相关问题