13

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

enter image description here

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?

Community
  • 1
  • 1
Martin AJ
  • 5,689
  • 5
  • 42
  • 92

4 Answers4

23

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

Chonchol Mahmud
  • 2,891
  • 5
  • 33
  • 68
8

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;
Community
  • 1
  • 1
Alexey Mezenin
  • 148,626
  • 22
  • 267
  • 261
  • Should I write those two first lines *(I use Apache)* in `.htaccess` file which exists on the root? – Martin AJ Nov 23 '16 at 05:40
  • @MartinAJ usually you put it in main config file or virtual host config. – Alexey Mezenin Nov 23 '16 at 05:42
  • Well yeah, there is a `/config` folder on the root. So? Should I create any file in it? – Martin AJ Nov 23 '16 at 05:44
  • anyway I guess this solution is the best but honestly I cannot implement it. I use cPanel web service and I really don't know where exactly should I paste your codes. – Martin AJ Nov 23 '16 at 06:34
  • 1
    This answer seems, to me, to be the best one. This will keep your files above the /public folder safe. As is Laravel's intention. This can be a bit more difficult or even impossible when transfering the app/website to a hosting service which won't allow you to edit your virtual host. So make sure you'll take a hosting provider which DOES allow you to edit your virtual host file(s). Or host it yourself :) – w00tland Sep 19 '18 at 10:47
  • should i make vhost or just can put `DocumentRoot "/path_to_laravel_project/public"` in .htaccess? – Ray Coder Oct 22 '20 at 11:04
  • Wait for a few minutes sometimes ubuntu 20.04 takes time, also clear the browser cache and restart – DragonFire Mar 02 '21 at 21:24
0

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';
Piyush
  • 27
  • 5
0

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>
DragonFire
  • 2,836
  • 1
  • 23
  • 40