Setup An .Htaccess File For Redirecting To Laravel’s Public Folder
In Laravel, when you install the framework, the default setup has the website's entry point in the /public folder. However, if you navigate to your site URL, you might see a list of Laravel files instead of the actual webpage.
To fix this, you can create a file called .htaccess in the main Laravel directory (where your public folder is). In simple terms, this file contains instructions for the server to handle web requests correctly.
Here is an example of what you can put in your .htaccess file:
Create a .htaccess file in your root directory and add the following code.
<IfModule mod_rewrite.c>
# That was ONLY to protect you from 500 errors
# if your server did not have mod_rewrite enabled
RewriteEngine On
# RewriteBase /
# NOT needed unless you're using mod_alias to redirect
RewriteCond %{REQUEST_URI} !/public
RewriteRule ^(.*)$ public/$1 [L]
# Direct all requests to /public folder
</IfModule>
This code tells the server to use the public folder as the entry point for web requests. It uses a feature called mod_rewrite to handle the redirection.
So, after adding this .htaccess file, when users access your Laravel site, they will be directed to the content in the /public folder, and they should see your actual webpage instead of a directory listing.
