14

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
  • 6,261
  • 8
  • 53
  • 111

3 Answers3

9

Do not modify any Laravel files. Instead configure your web server (Apache or Nginx) to point to the Laravel project's 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;

Having your document root as /path_to_laravel_project/ alone will create serious security risks, potentially opening your entire app configuration to the internet.

miken32
  • 42,008
  • 16
  • 111
  • 154
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 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

For running Apache on Ubuntu, do the following. This assumes you have Composer and PHP already installed.

# create your web directory
sudo mkdir -p /var/www/example.com
# install laravel
cd /var/www/example.com
composer create-project laravel/laravel example-app
# proper ownership
sudo chown -R www-data:www-data /var/www/example.com
# create a configuration
sudo nano /etc/apache2/sites-available/example.com.conf

Then in the file paste

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    # SSL configuration, etc.
</VirtualHost>

Then run

# enable your site
sudo a2ensite <your domain>
# disable the default site
sudo a2dissite 000-default
# restart the web server
sudo systemctl reload apache2
miken32
  • 42,008
  • 16
  • 111
  • 154
DragonFire
  • 3,722
  • 2
  • 38
  • 51
-1

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
  • 35
  • 1
  • 5