Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single route giving a 403 Forbidden error in Laravel 4

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:

So the routes in my project all work except for one, that route being {mywebsite}/admin, which gives me a 403 error. It does work 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).

Here is my routes.php file:

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:

<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>

Does anyone have any clue why this might be happening?

like image 549
Brian Avatar asked Jul 26 '14 19:07

Brian


People also ask

What is a 403 Forbidden error?

Here are some examples of 403 error messages: Often, 403 forbidden errors are caused by an access misconfiguration on the client-side, which means you can usually resolve the issue yourself. A common cause of these errors is the file or folder permission settings, which control who can read, write, and execute the file or folder.

Why can’t I access the Laravel website’s admin page?

With the clue that you can access {mywebsite}/index.php/admin while other routes work fine means that your Laravel route and .htaccess file are working. So the problem is probably from .htaccess skipping url rewrite only for url {mywebsite}/admin, related to these three lines:

What does 403 mean on a website?

Clear Your Web History/Cache 403 is an HTTP status code, which is a standard response code from the web server to the client’s browser. When there is an error, these codes communicate the cause of the problem so that users know why the page isn’t loading.

Why do I get a 403 error in Windows 10?

Reset File and Directory Permissions. Another reason for encountering a 403 Error Forbidden message is bad permissions for your files or folders. In general, when files are created, they come with certain default permissions. These basically control how you can read, write, and execute the files for your use.


1 Answers

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

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

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 /admin folder but it is prohibited to do so.

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.

like image 57
Unnawut Avatar answered Sep 18 '22 13:09

Unnawut