在 Laravel 4 中给出 403 Forbidden 错误的单一路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24974578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:52:09  来源:igfitidea点击:

Single route giving a 403 Forbidden error in Laravel 4

laravellaravel-4laravel-routing

提问by Brian

I have been trying to figure out why this is happening for the past couple of days with no success. I found some other questions dealing with 403 errors while routing in Laravel but none pertaining to a problem with a single route. Somewhat new to Laravel and web development, so might be missing something obvious, but here goes:

在过去的几天里,我一直试图弄清楚为什么会发生这种情况,但没有成功。在 Laravel 中进行路由时,我发现了一些处理 403 错误的其他问题,但没有一个与单个路由的问题有关。Laravel 和 web 开发有点新,所以可能会遗漏一些明显的东西,但这里是:

So the routes in my project all work except for one, that route being {mywebsite}/admin, which gives me a 403 error. It doeswork when I go to {mywebsite}/index.php/admin. What I don't understand is why none of my other routes have the same problem. For example, {mywebsite}/test works and {mywebsite}/admin/categories works. This is literally the only route that does not work. Also worth noting is that the same issue comes up when trying to access it on both my local server and my production server (Digital Ocean with Laravel Forge).

所以我的项目中的所有路由都有效,除了一个路由是 {mywebsite}/admin,它给了我一个 403 错误。当我转到 {mywebsite}/index.php/admin 时它确实有效。我不明白的是为什么我的其他路线都没有同样的问题。例如,{mywebsite}/test 有效,而 {mywebsite}/admin/categories 有效。这实际上是唯一行不通的路线。另外值得注意的是,当尝试在我的本地服务器和我的生产服务器(带有 Laravel Forge 的 Digital Ocean)上访问它时,会出现同样的问题。

Here is my routes.php file:

这是我的 routes.php 文件:

Route::get('/', function()
{
    return View::make('hello'); //works
});

Route::get('/admin', function()
{
    return "admin"; //403 error
});

Route::get('/test', function()
{
    return "test"; //works
});


//these all work
Route::get('/admin/dashboard', 'TaskCategoriesController@showAdmin');

// all category routes
Route::get('/admin/categories/', 'TaskCategoriesController@show');
Route::get('/admin/categories/{id}/edit', 'TaskCategoriesController@edit');
Route::post('/admin/categories/{id}/edit/', array('uses' =>    'TaskCategoriesController@update'));
Route::post('/admin/categories/create/{id}', array('uses' => 'TaskCategoriesController@create'));
Route::get('/admin/categories/delete/{id}', array('uses' => 'TaskCategoriesController@delete'));

Route::get('/admin/categories/create', function()
{
    return View::make('CreateCategory');
});

// all form routes
Route::get('/admin/forms/{id}/edit', 'TaskFormsController@edit');
Route::post('/admin/forms/{id}', 'TaskFormsController@create');
Route::post('/admin/forms/{id}/submit', 'OrdersController@submitOrder');
Route::get('/admin/forms/{id}/add', 'TaskFormsController@addFormElement');
Route::get('/admin/forms/{id}/edit/{elementId}', 'TaskFormsController@editFormElement');
Route::get('/admin/forms/{id}/delete/{elementId}', 'TaskFormsController@deleteFormElement');
Route::post('/admin/forms/{id}/saveUpdates/{tid}', 'TaskFormsController@updateFormElement');


//time table routes
Route::post('/admin/categories/{id}/timetable/{date}', array('uses' => 'TimeTableController@updateTimetable'));
Route::get('/admin/categories/{id}/timetable', array('uses' => 'TimeTableController@timetable'));
Route::get('/admin/categories/{id}/timetable/{date}', array('uses' => 'TimeTableController@editWeekTable'));

And here is my .htaccess file:

这是我的 .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>

Does anyone have any clue why this might be happening?

有谁知道为什么会发生这种情况?

回答by Unnawut

With the clue that you can access {mywebsite}/index.php/adminwhile other routes work fine means that your Laravel route and .htaccess file are working.

有了你可以访问{mywebsite}/index.php/admin而其他路由正常工作的线索意味着你的 Laravel 路由和 .htaccess 文件正在工作。

So the problem is probably from .htaccess skipping url rewrite only for url {mywebsite}/admin, related to these three lines:

所以问题可能来自 .htaccess skipping url rewrite only for url {mywebsite}/admin,与这三行有关:

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

The first two lines above tells apache not to url rewrite to Laravel's index.php if the requested url points to an existing file or folder. The fact that it is showing 403 Forbidden is most probably because apache is trying to directory-list /adminfolder but it is prohibited to do so.

如果请求的 url 指向现有文件或文件夹,上面的前两行告诉 apache 不要将 url 重写到 Laravel 的 index.php。它显示 403 Forbidden 的事实很可能是因为 apache 正在尝试目录列表/admin文件夹,但禁止这样做。

So the solution is to make sure that you do not have a folder app/public/admin. If there is, delete it and try again.

因此,解决方案是确保您没有文件夹app/public/admin。如果有,请删除它并重试。