Monday, January 29, 2024

Laravel : Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.php,index.html) found, and server-generated directory index forbidden by Options directive, referer

 Error Message

Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.php,index.html) found, and server-generated directory index forbidden by Options directive, referer

When?

When we try to host a Laravel application in the PHP container, we have faced the above-mentioned issue while accessing the application root URL.

The reason for this error message

When we access the application URL, the root folder does not have any index.html or index.php files. The index.php file will be available in the /public subfolder folder.

That is the reason we used to get this error.

 

For security reasons, we will stop listing folder structures by using the following option: Which will prevent showing files and folders if there is no index file.

 

Options -Indexes

 

If we set the following option, the error will go off. start lighting folders and files, but the application will not work.

Options +Indexes

 

A proper fix for this error message

 

There are multiple ways we can fix this issue.

 

Method 1 Set the root folder to public (/var/www/html/public) in the httpd.conf file.

Method 2 Use the following code in the root.htaccess file

<IfModule mod_rewrite.c>

    Options +FollowSymlinks

    RewriteEngine On

                RewriteCond %{REQUEST_FILENAME} !-f

                RewriteCond %{REQUEST_FILENAME} !-d

                RewriteCond %{REQUEST_URI} !^public

                RewriteRule ^(.*)$ public/$1 [L]

</IfModule>