简体   繁体   中英

How to make public folder as root in Laravel?

I use Laravel framework. As you know, its directory looks like this:

在此处输入图片说明

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

http://example.com/public

anyway, only my domainname ( http://example.com/ ) isn't my root. How can I make /public folder as root?


Curently I've found a workaround. I can create an index.php on the root and write a redirect code to /public folder. So when user enters http://example.com/ , it will be redirected to http://example.com/public automatically. But still that's ugly. I don't like to see /public in the URL. Any suggestion?

There are many ways to do this.

You just need to cut index.php and .htaccess from public directory and paste it in the root directory, that's all and replace two lines in index.php as

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

Best way to do is .htaccess . Create .htaccess file in root directory in Laravel installation. And the below code will work for this.

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

You can read from here: WAY TO DO

Do not mofidy any Laravel files. Use web server (Apache or Nginx) to point Laravel project to 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;

Step 1: Put your /public/index.php and /public/htaccess file to your root directory as /index.php and /htaccess .
Step 2: Now Make changes in your index.php

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

to

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

For Ubuntu 20.04 do the following for virtual hosts. Also after doing wait for few minutes, clear the browser cache and reload.

sudo mkdir /var/www/<your domain>/public
sudo chown -R $USER:$USER /var/www/<your domain>
sudo nano /etc/apache2/sites-available/<your domain>.conf

Then in the file paste

<VirtualHost *:80>
    ServerName <your domain>
    ServerAlias www.<your domain>
    ServerAdmin <your email address>
    DocumentRoot /var/www/<your domain>/public
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then run

sudo a2ensite <your domain>
sudo a2dissite 000-default
sudo systemctl reload apache2

nano /var/www/<your domain>/public/index.html

Then paste in file

<html>
  <head>
    <title><your domain></title>
  </head>
  <body>
    <center>
        <p>Site Under Maintenance</p>
    </center>
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM