Avoid public folder of laravel and open directly the root in web server

93,830

Solution 1

Let's assume you have this folder structure in your server

.cpanel/
public_html/
public_ftp/
..

And the laravel folder structure is

app/
bootstrap/
public/
vendor/
composer.json
artisan
..

You can create a folder name mylaravelsite on your server inline with public_html and public_ftp folder, and copy to it the whole laravel application except the public folder because you will paste all of it contents on the public_html, so you have now:

.cpanel/
public_html/
public_html/packages
public_html/vendor
public_html/index.php
public_html/.htaccess
...
public_ftp/
mylaravelsite/
mylaravelsite/app
mylaravelsite/bootstrap
...

On your public_html/index.php change the following line:

require __DIR__.'/../bootstrap/autoload.php';

$app = require_once __DIR__.'/../bootstrap/start.php';

to

require __DIR__.'/../mylaravelsite/bootstrap/autoload.php';

$app = require_once __DIR__.'/../mylaravelsite/bootstrap/start.php';

and also don't forget to change /mylaravelsite/bootstrap/paths.php public path, you might use it.

'public' => __DIR__.'/../public',

to

'public' => __DIR__.'/../../public_html',

Your site should be running.

Solution 2

Create .htaccess in root folder and write these line

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

For production server it is recommended that you point your domain directly to the public folder

Solution 3

It sounds like the information you are missing is:

Documentroot

This is also called the "web root". Apache specifically calls it the DocumentRoot - Click that link for the official explanation.

Basically what it does is set which directory the server pulls files from. In Laravel, the document root should be the public folder.

Setting the DocumentRoot to public means that going to http://mylaravelsite.com in the browser will infact be "pointing" to the public folder as you want.

In the .htaccess file (actually, more likely in the virtual host configuration), you can set the DocumentRoot for your site:

DocumentRoot /path/to/laravel-app/public

How you set this up depends on your hosting. Each hosting has different ways to setup a website and so we cannot answer that specifically for you - you should consult your web hostings tech support. The key point is that the public directory should be the web root, while everything else should be "behind the web root".

Fair warning tho: some hosting providers do not give you enough access to accomplish that.

Solution 4

Just add .htaccess file on Laravel root if it's not present and add following lines in it, that's it,

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

Solution 5

create .htacess file in the root directory of your laravel project and put this code in.

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

Share:
93,830
Sagiruddin Mondal
Author by

Sagiruddin Mondal

Researcher &amp; Developer in Parabole LLC. Founder of Dolovers.com Please be a member of my site Click me to join the Debating Website. This site's algorithm promises the support to meaningful communications. Thanks ! Java Play Framework Neo4j Ontology JavaScript HTML5 &amp; CSS3 mysql

Updated on July 09, 2022

Comments

  • Sagiruddin Mondal
    Sagiruddin Mondal almost 2 years

    I was going through all possible sample on internet to solve this. Still it is an headache.

    I just want to avoid the 'public' in www.mylaravelsite.com/public/ and make it like www.mylaravelsite.com for the root directory.

    Now I do not want to avoid the security concern,So I learned .htaccess would be the best way.

    Any solution friends ?

    & advance thanks for interacting !

  • Sagiruddin Mondal
    Sagiruddin Mondal over 10 years
    Thank you for your kind reply bro. I want to upload it on web server, in this case what should I do ?
  • Sagiruddin Mondal
    Sagiruddin Mondal over 10 years
    Bro I have done everything as per your instructions, It is showing 404 page instead . what should I do ?
  • lukaserat
    lukaserat over 10 years
    I've checked your site: dolovers.com/sagir, you already installed laravel successfully. I've also noticed you have route error. Specify your default root (/). See the documentation: Laravel Routing
  • Sagiruddin Mondal
    Sagiruddin Mondal over 10 years
    thank you brother, I was having trouble with the php versions as web hosing was not supporting the 5.3 or greater versions directly. And brother that was a old address of my site, pls create an account on my new alpha testing of this app , dolovers.com/auth/signup. thanks for help :)
  • Brandon Clapp
    Brandon Clapp almost 9 years
    This worked for me. Just move the .htaccess and index.php files from the public directory into the root directory and change the bootstrap paths.
  • Daniel Steiner
    Daniel Steiner about 8 years
    No, no and no. Don't do that. The reason why Laravel has the document root in a subdirectory is so that you don't have to expose your code to the public.
  • Daniel Steiner
    Daniel Steiner about 8 years
    You have to change your apache config which you can't since you're hosting on a webhosting package instead of a virtual server or so.
  • Hola
    Hola almost 8 years
    Is that the solution for laravel 4 or 4.2 because I couldn't find the /mylaravelsite/bootstrap/paths.php in my root directory and I'm using laravel 5.2
  • Hola
    Hola almost 8 years
    Can you give solution for 5+ please , because I'm facing some difficulty with the latest version.
  • stef
    stef about 7 years
    My osx development environment required a leading slash in front of "public/$1"
  • lukaserat
    lukaserat about 7 years
    Not anymore @BakhtawarGIll Please see their documentation. Laravel changes their directory structure.
  • Yousef Altaf
    Yousef Altaf almost 7 years
    For Laravel 5.2 just add this lines into public/index.php $app->bind('path.public', function() { return __DIR__; });
  • Puneet Verma
    Puneet Verma about 5 years
    simplest solution. Thanks
  • MohcinBN
    MohcinBN almost 5 years
    Thank you, your answer helped me a lot, i search for this answer two days
  • frankfurt-laravel
    frankfurt-laravel over 4 years
    Same suggestion which Anik gave.
  • MItrajyoti
    MItrajyoti over 3 years
    Good. A little more change removes the index.php too.
  • German Katz
    German Katz almost 3 years
    Thanks man, this was the only .htaccess that make my project work!