How to make a public folder as root in Laravel?

I am using the Laravel framework. As you know, its directory is as follows:

enter image description here

To open the home page of my site ( Route::get('/', ' HomeController@index '); ), I need to open the /public folder of the directory. In other words, to see the first page of my site, here is the URL:

 http://example.com/public 

In any case, only my domain name ( http://example.com/ ) is not my root. How can I make the /public folder as root?


In short, I found a workaround. I can create index.php in the root and write the redirect code to the /public folder. Therefore, when a user enters http://example.com/ , he will be automatically redirected to http://example.com/public . But still it is ugly. I do not like to see /public in the URL. Any suggestion?

+8
source share
3 answers

There are many ways to do this.

You just need to cut out index.php and .htaccess from the public directory and paste it into the root directory to get everything and replace the two lines in index.php with

 require __DIR__.'/bootstrap/autoload.php'; $app = require_once __DIR__.'/bootstrap/app.php'; 

The best way to do this is .htaccess . Create a .htaccess file in the root directory when installing Laravel. And the code below will work for this.

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

You can read here: WAY TO DO

+11
source

Do not modify any Laravel files. Use a web server (Apache or Nginx) to point the Laravel project to the public directory .

For Apache, you can use these directives:

 DocumentRoot "/path_to_laravel_project/public" <Directory "/path_to_laravel_project/public"> 

For nginx you should change this line:

 root /path_to_laravel_project/public; 
+3
source

Step 1. Place the file /public/index.php and / public / htaccess in the root directory as /index.php and / htaccess.
Step 2. Now make changes to your index.php

 require __DIR__.'/../bootstrap/autoload.php'; //OLD code $app = require_once __DIR__.'/../bootstrap/app.php'; 

in

  require __DIR__.'./bootstrap/autoload.php'; //NEW code $app = require_once __DIR__.'./bootstrap/app.php'; 
0
source

Source: https://habr.com/ru/post/1260187/