Home > Software design >  how to remove the /public folder from laravel 9?
how to remove the /public folder from laravel 9?

Time:08-02

I have laravel 9 running locally on windows (MAMP) and I want to open the home page with:

localhost/myproject

instead of

localhost/myproject/public

I tried to add .htaccess to the root folder with the following:

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

and it didn't work! (I got 404), (Although this worked with laravel 8!)

so what is the solution for this !? I spent hours on searching blogs and tweaking to no avail! I also read on some discussions that using .htaccess or replacing the server.php and renaming it are NOT totally safe for production.

so what is the solution, and would this solution also work if I host my website online (for example on shared hosting) ?

THANKS

CodePudding user response:

I have deployed several websites to hosting and vps servers with following .htaccess file:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

If error still occurs you may contact to your hosting provider and ask them to give you valid .htaccess rules. You may move your files inside public folder to root folder as well.

CodePudding user response:

To remove the public folder from laravel 9 you need to customize the .htaccess file. You must have mod_rewrite enabled on your Apache server. The rewrite module is required to apply these settings. You also have enabled .htaccess in Apache virtual host for Laravel.

Update the code into your .htaccess file:

<IfModule mod_rewrite.c>
   RewriteEngine On 
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
  • Related