3

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> 
1
  • You need to check your server's error log for the details of this 500 error. Do you have any other .htaccess files? (You should have another .htaccess file at /public/.htaccess. "or /public.index.php" - presumably you mean /public/index.php? Commented Mar 11, 2022 at 8:57

2 Answers 2

8

How to remove public from url in laravel 9

  • create a .htaccess file in root directory

  • Add this code

    RewriteEngine On RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ ^$1 [N] RewriteCond %{REQUEST_URI} (\.\w+$) [NC] RewriteRule ^(.*)$ public/$1 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ server.php 
  • create server.php in the root directory

  • Add this code

    $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'; 
Sign up to request clarification or add additional context in comments.

3 Comments

I saw several responses here in multiple SO questions... only this one worked for me. Thanks man!
Same here, only this solution worked, one reason can be that the laravel site itself was in a subdirectory. None of the other solutions take that into account
It still allows website to be access throught /public/ can we remove this as well, so it only works without it? It should never with public in URL.
4
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.