'Remove public from URL in Laravel 9 using .htaccess on shared hosting/CPanel hosting

I am trying to host a Laravel 9 application on a Cpanel shared host. However, the application keeps returning the below error even when I try to access https://example.com/public. Also below is my .htaccess file at the root; it used to work perfectly with Laravel 8 but is not working anymore. My problem is I want to access my application just using the domain without /public or /public.index.php (e.g., example.com).

Internal server error 500

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>


Solution 1:[1]

RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

You are missing the slash prefix on the CondPattern !^public, so this condition will always be successful and repeatedly rewrite the request, potentially causing a rewrite loop (ie. 500 error).

It should be like this instead:

RewriteCond %{REQUEST_URI} !^/public($|/)
RewriteRule ^(.*)$ public/$1 [L]

HOWEVER, you should also have another .htaccess file with mod_rewrite directives in the /public subdirectory that manages the routing through your Laravel app, and this should prevent a rewrite-loop.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrWhite