我一直在试图找出过去几天发生这种情况的原因并没有成功.我在Laravel中路由时发现了一些其他问题来处理403错误,但没有一个问题涉及单个路由的问题. Laravel和Web开发有点新,所以可能会
所以我的项目中的路由都工作,除了一个,该路由是{mywebsite} / admin,这给了我403错误.当我去{mywebsite} /index.php/admin时,它确实有效.我不明白为什么我的其他路线都没有同样的问题.例如,{mywebsite} / test有效,{mywebsite} / admin / categories有效.这实际上是唯一不起作用的路线.另外值得注意的是,尝试在我的本地服务器和生产服务器(带有Laravel Forge的Digital Ocean)上访问它时会出现同样的问题.
这是我的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'));
这是我的.htaccess文件:
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine On # Redirect Trailing Slashes... RewriteRule ^(.*)/$/$1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
有没有人知道为什么会发生这种情况?
有了线索,您可以访问{mywebsite} /index.php/admin,而其他路由正常工作意味着您的Laravel路由和.htaccess文件正常工作.所以问题可能来自于.htaccess仅为url {mywebsite} / admin跳过url重写,与这三行相关:
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L]
如果请求的url指向现有文件或文件夹,则上面的前两行告诉apache不要重写到Laravel的index.php.显示403 Forbidden的事实很可能是因为apache正在尝试使用directory-list / admin文件夹,但是禁止这样做.
因此,解决方案是确保您没有文件夹app / public / admin.如果有,请将其删除并重试.