Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 !

like image 788
Sagiruddin Mondal Avatar asked Nov 12 '13 07:11

Sagiruddin Mondal


People also ask

What is the Public folder in Laravel?

The storage/app/public directory may be used to store user-generated files, such as profile avatars, that should be publicly accessible. You should create a symbolic link at public/storage which points to this directory. You may create the link using the php artisan storage:link Artisan command.

Where is the root directory in Laravel?

The best way to retrieve your public folder path from your Laravel config is the function: $myPublicFolder = public_path(); $savePath = $mypublicPath. "enter_path_to_save"; $path = $savePath. "filename.

What is the root directory of Laravel project?

The Root Directory Structure of LaravelThe config directory holds all your project configuration files (. config). The database directory contains your database files. The public directory helps start your Laravel project and maintains other necessary files such as JavaScript, CSS, and images of your project.


4 Answers

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.

like image 170
lukaserat Avatar answered Oct 16 '22 07:10

lukaserat


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

like image 43
Anik Avatar answered Oct 16 '22 08:10

Anik


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.

like image 24
fideloper Avatar answered Oct 16 '22 07:10

fideloper


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>
like image 35
Atul Shah Avatar answered Oct 16 '22 09:10

Atul Shah