66

My laravel installation was working fine yesterday but today I get the following error:

Forbidden

You don't have permission to access / on this server.

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

Does anyone know where I am going wrong?

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
panthro
  • 22,779
  • 66
  • 183
  • 324

26 Answers26

113

Create and put this .htaccess file in your laravel installation(root) folder.

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Alexey
  • 7,127
  • 9
  • 57
  • 94
41

For me, It was just because I had a folder in my public_html named as same as that route!
so please check if you don't have a folder with the address of that route!
hope be helpful

Ali
  • 1,525
  • 1
  • 16
  • 27
  • 2
    This works for me, Don't now how long I would have been stuck on it if I didn't have seen this answer. woah relief.. ^^ – Rishabh Jun 02 '21 at 08:53
  • This works for me too! Thanks! But do you have a link to where it states the reason. – Adedoyin Akande Sep 21 '21 at 09:53
  • 2
    @AdedoyinAkande your welcome bro! so happy for helping you, actually, because the webserver which maybe apache is performing folder permissions before the laravel routes to be registered in RouteSerivceProvider.php so you will see that error – Ali Sep 21 '21 at 12:02
  • 1
    @Ali Okay, I get it now, many thanks! – Adedoyin Akande Sep 22 '21 at 15:01
  • 2
    oh ffs! After hours of confusion this was the answer! I had a folder in the /public/ folder by the same name – Leon Nov 07 '21 at 16:06
24

I met the same issue, then I do same as the solution of @lubat and my project work well. :D My virtualhost configuration:

<VirtualHost *:80>
     ServerName laravelht.vn
     DocumentRoot D:/Lavarel/HTPortal/public
     SetEnv APPLICATION_ENV "development"
     <Directory D:/Lavarel/HTPortal/public>
         DirectoryIndex index.php
         AllowOverride All
         Require all granted
         Order allow,deny
         Allow from all
     </Directory>
 </VirtualHost>
Hieu Tran AGI
  • 849
  • 9
  • 9
20

Check your virtual host configuration. For Ubuntu you could find it at /etc/apache2/sites-available/yourlaravel.conf It should be like this:

<VirtualHost *:80>
ServerName yourlaravel.com
DocumentRoot "/path/to/your/laravel/project/public"
ServerAlias *.yourlaravel.com

<Directory "/path/to/your/laravel/project/public">
    AllowOverride All
    Require all granted
</Directory>

The key line is Require all granted inside <Directory>.

Hope it helps!

lubart
  • 1,746
  • 3
  • 27
  • 35
15

Have you tried to change the .htaccess file that laravel suggested if the default one doesn't work? I had this similar problem and changed it to

Options +FollowSymLinks
RewriteEngine On

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

and it soleved :)

15

Just add a .htaccess file in your root project path with the following code to redirect to the public folder:

.HTACCESS

## Redirect to public folder
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ public/ [L]
    RewriteRule (.*) public/$1 [L]
</IfModule>

Very simple but work for me in my server.

Regards!

Radames E. Hernandez
  • 4,235
  • 27
  • 37
  • I added an .htaccess file in my Laravel Root foler with the contents you posted but I am still getting the 403 Forbidden page...help! – CodeConnoisseur May 19 '19 at 17:47
  • I had redirected in with another .htaccess which worked on another host but here your htaccess worked for me thanks. – Steve Moretz Feb 08 '20 at 09:38
  • For readers in legacy projects. This works with php 7.4 and Laravel 5.8 if you are exposing the project. – jogarcia Mar 13 '23 at 01:33
10

It was solved for me with the Laravel default public/.htaccess file adding an extra line:

The /public/.htaccess file remains as follows:

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

    RewriteEngine On
    DirectoryIndex index.php # <--- This line

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Evhz
  • 8,852
  • 9
  • 51
  • 69
9

For those who using Mamp or Mamp pro:

Open MAMP Pro
Click on “Hosts”
Click on “Extended” (UPDATE: Only if you are using MAMP Pro 3.0.6)
Check “Indexes”
Click “Save”
That’s it! Reload your localhost starting page and it should work properly.
DPP
  • 12,716
  • 3
  • 49
  • 46
7

With me the problem appeared to be about the fact that there was no index.php file in the public_html folder. When I typed in this address however: http://azxcvfj.org/public , it worked (this address is just an example. It points to nowhere). This made me think and eventually I solved it by doing the following.

I made a .htaccess file in the app's root folder (the public_html folder) with this contents:

<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 ^ public/index.php [L]
</IfModule>

And this worked. With this file you are basically saying to the server (Apache) that whenever someone is trying to access the public html folder(http://azxcvfj.org) that someone who is being redirected is redirected to http://azxcvfj.org/public/index.php

PieterVK
  • 1,481
  • 1
  • 10
  • 4
6

First, update your Virtual Host configuration;

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html/example-project/public
    <Directory /var/www/html/example-project/public/>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Then, change both permission and ownership of the asset as illustrated below.

$ sudo chgrp -R www-data /var/www/html/example-project
$ sudo chmod -R 775 /var/www/html/example-project
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Yves
  • 807
  • 15
  • 17
6

In your VirtualHost write the DocumentRoot to point to your Laravel Application in your home directories. In addition you must add the Directory shown below, with your own path:

<Directory /home/john/Laravel_Projects/links/public/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    Require all granted
</Directory>

The second step, you must go to your Laravel Project and run the following command.

sudo chmod -R 777 storage bootstrap/cache

At the end restart your apache2:

sudo service apache2 restart
  • 1
    i think need to access storage folder to www-data user sudo chmod -R www-data /var/www/html/example-project/storage && sudo chmod -R 755 /var/www/html/example-project/storage – saber tabatabaee yazdi Aug 26 '20 at 05:37
  • 1
    Yes, 755 should be enough for the chmod. Don't use 777 when it's not necessary (security reasons). – J.D. Sandifer Oct 26 '20 at 17:45
1

This is solved my problem by adding this line:

DirectoryIndex index.php
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
1

In my case, this error was due to internal links in the server, PHP wasn't able to access those folders unless the proper option for that was added to .htaccess.

Add the following line after RewriteEngine On:

Options +FollowSymLinks
António Almeida
  • 9,620
  • 8
  • 59
  • 66
0

Chances are that, if all the answers above didn't work for you, and you are using a request validation, you forgot to put the authorization to true.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class EquipmentRequest extends FormRequest {
  /**
   * Determine if the user is authorized to make this request.
   *
   * @return bool
   */
  public function authorize() {
    /*******************************************************/
    return true; /************ THIS VALUE NEEDS TO BE TRUE */
    /*******************************************************/
  }

  /* ... */
}
Amin NAIRI
  • 2,292
  • 21
  • 20
0

If you have tried all .htaccess answers from comments and none of them worked, it's possible that actually you have bad APP_URL in you .env config file. That worked for me.

0

For my case was encountering this issue with Laravel 6.x and managed to sort it out by installing an SSL Certificate. Tried playing around with .htaccess but never worked. I'm using the default Laravel 6.x .htaccess file which has the following contents

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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

Check your project root directory index.php file. I think your index.php file missing.

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/public/index.php';

then check your project root directory .htaccess file

<IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews -Indexes
        </IfModule>
    
        RewriteEngine On
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]
    
        # Send Requests To Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>

I hope your problem solved thank you.

0

Remember that Laravel's project root is /public

The solution for me was so simple it was silly - I had moved my folders and updated the paths in my Apache server's virtual hosts configuration (extra/httpd-vhosts.conf for XAMPP), but had forgotten that Laravel's app root is located at /public. Simply updating:

D:\Users\Me\Documents\Projects\Websites\myproject

to

D:\Users\Me\Documents\Projects\Websites\myproject\public

...and then restarting the server solved the issue.

Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68
0

In my case, the index.php file was missing from the Laravel root project, this also caused 403 error forbidden, creating a new file and put this code on it got the job done.

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);


WardNsour
  • 313
  • 1
  • 2
  • 16
-1

I had the same problem and builded a .htaccess file to fix this issue with a few more improvements. You can use it as it is, just download it from Github Gist and change the filename "public/index.php" to "public/app.php" and see how it works! :)

dude
  • 5,678
  • 11
  • 54
  • 81
-1

For me this was simply calling route() on a named route that I had not created/created incorrectly. This caused the Forbidden error page.

Restarting the web server allowed proper Whoops! error to display:

Route [my-route-name] not defined.

Fixing the route name ->name('my-route-name') fixed the error.

This is on Laravel 5.5 & using wampserver.

Andrew
  • 18,680
  • 13
  • 103
  • 118
-1

I had this error and i just pasted the http://127.0.0.1:8000/ directly into the url bar. you get the below when you type : php laravel serve

i was putting in localhost/http://127.0.0.1:8000/ to get the error you mentioned.

DonQuery
  • 47
  • 7
-1

On public/.htaccess edit to

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

          RewriteEngine On

          # Redirect Trailing Slashes If Not A Folder...
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_URI} (.+)/$
          RewriteRule ^ %1 [L,R=301]

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

          # Handle Authorization Header
          RewriteCond %{HTTP:Authorization} .
          RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

In the root of the project add file

Procfile

File content

web: vendor/bin/heroku-php-apache2 public/

Reload the project to Heroku

bash
heroku login
cd my-project/
git init
heroku git:remote -a my project
git add .
git commit -am "make it better"
git push heroku master
heroku open
Theo
  • 57,719
  • 8
  • 24
  • 41
-1

The problem is the location of the index file (index.php).You must create a symbolic link in the root of the project to public/index.php with the command "ln-s public/index.php index.php"

Heretic Sic
  • 364
  • 2
  • 9
-2

You might be facing the file permissions issue. Verify your htacces file, did it change from yesterday ? Also, if you were doing any "composer update" or "artisan optimize" stuff, try chowning your laravel project folder for your username.

chown -R yourusername yourlaravelappfolder 

EDIT: the problem is possibly due to your local file permissions concerning Vagrant. Try to

set the permissions to the Vagrantfile containing folder to 777
Gadoma
  • 6,475
  • 1
  • 31
  • 34
  • I havent changed anything since yesterday. Thanks for your suggestion but it did not work. – panthro Aug 16 '13 at 11:54
  • OK, is it possible that your server environment changed in the meantime ? Something must've happened :) The error you are facing has to be for some reason, right? – Gadoma Aug 16 '13 at 11:59
  • Nothing has changed. It was working last night. Shut the computer down. It's not working this morning. – panthro Aug 16 '13 at 12:01
  • where are you hosting your app ? locally or on a remote server ? – Gadoma Aug 16 '13 at 12:06
  • Locally using VM on vagrant – panthro Aug 16 '13 at 13:22
  • I've changed the permissions for the vagrantfile - but still no luck. – panthro Aug 16 '13 at 13:51
  • anyway, it seems to me as a vagrant issue. Try here: https://github.com/bryannielsen/Laravel4-Vagrant / http://forums.laravel.io/viewtopic.php?id=10446 / http://culttt.com/2013/06/17/setting-up-vagrant-with-laravel-4/ – Gadoma Aug 16 '13 at 14:02
-2

I've found solution

I put following code into my public/.htaccess and now its running at rocket speed :D

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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