To properly configure your Laravel site on a server (whether cPanel, NGINX, or Apache), follow these steps:
1. Create .htaccess in the Root Directory:
In your root project directory (not the public folder), create a .htaccess file with the following content:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Redirect requests that are not for existing files or directories to /public
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1
# Fallback to index.php
RewriteRule ^(/)?$ public/index.php [L]
</IfModule>
2. Create .htaccess in the public Directory:
Inside the public folder, create another .htaccess file with the following content:
<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>