47

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
  • 5,407
  • 2
  • 29
  • 44
  • https://www.youtube.com/watch?v=ybJYyU5FPv4 Watch this :D – MD Singh Mar 19 '16 at 05:47
  • https://stackoverflow.com/questions/20547891/how-to-run-laravel-in-root-directory-without-the-public-folder/65716511#65716511 use this link for better result. – pankaj Jan 14 '21 at 09:42
  • Try **My Solution** It will work **everywhere**. check Link https://stackoverflow.com/questions/25222509/laravel-not-detecting-files-from-public-folder/69517681#69517681 – pankaj Oct 10 '21 at 18:03

10 Answers10

59

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.

Sam
  • 4,205
  • 2
  • 17
  • 13
lukaserat
  • 4,768
  • 1
  • 25
  • 36
  • 2
    Bro I have done everything as per your instructions, It is showing 404 page instead . what should I do ? – Sagiruddin Mondal Nov 14 '13 at 10:28
  • I've checked your site: http://www.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](http://laravel.com/docs/routing) – lukaserat Nov 15 '13 at 01:46
  • 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 , http://dolovers.com/auth/signup. thanks for help :) – Sagiruddin Mondal Nov 15 '13 at 09:15
  • 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 Jun 20 '16 at 03:10
  • Can you give solution for 5+ please , because I'm facing some difficulty with the latest version. – Hola Jun 21 '16 at 06:06
  • Not anymore @BakhtawarGIll Please see their [documentation](https://laravel.com/docs/5.4/structure). Laravel changes their directory structure. – lukaserat Mar 20 '17 at 06:20
  • For Laravel 5.2 just add this lines into `public/index.php` `$app->bind('path.public', function() { return __DIR__; });` – Yousef Altaf Jul 11 '17 at 13:21
34

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

Anik
  • 441
  • 4
  • 7
16

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.

fideloper
  • 12,213
  • 1
  • 41
  • 38
7

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>
Atul Shah
  • 79
  • 1
  • 2
7

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

Imad Ullah
  • 929
  • 9
  • 17
5

point your web directory to public/. If you are using apache this will work:

<VirtualHost *:80>
  DocumentRoot /var/www.mylaravelsite.com/public/
  ServerName www.mylaravelsite.com
</VirtualHost>
lukaserat
  • 4,768
  • 1
  • 25
  • 36
3

Here is the solution, But Must not do it in real projects, it is just for starter to test Laravel and i have tested it with laravel 5.0 and it is ,cut index.php and .htaccess files from public folder and paste it in the root directory and change these two lines as

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

It is Highly Recommended not to use the above method without testing environment, and should use virtual host instead.

Muhammad Sadiq
  • 1,147
  • 11
  • 13
  • 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. – Brandon Clapp May 14 '15 at 00:32
  • 6
    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 Apr 26 '16 at 11:51
1

All you just need to change .env file in your laravel project files. if your laravel project inside in

Root/domain/public_html/laravel/

Go to .env file and edit

you need to edit app url path which should be

APP_URL=http://www.domainname.com/laravel/public/

Thats it your project will run now. if your laravel files is in public_html, then your app url must be like this

APP_URL=http://www.domainname.com

Thanks

0

For Lumen

Let's think you have a folder path like: /var/www/project/microservice(lumen src)

/var/www => document root for localhost/

Target you want: localhost/project/microservice[/foo...] => localhost/project/microservice/public/foo...

I do this by .htaccess (placed at /project folder) like below:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*/public/.*
RewriteCond %{REQUEST_URI} ^(/[^/]+/[^/]+/).*
RewriteRule ^[^/]+(.*)$ %1public$1 [L,R=301,P]
0

On Server(Apache) Create .htaccess in the root folder and write below line

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

Lean more on Gist

On Local Development You can create a virtual host by adding the below line to your apache vhost config file. Generally, you can find this file in C:\xampp\apache\conf\extra\httpd-vhosts.conf file

<VirtualHost *:80>
  DocumentRoot "C:/xampp/htdocs/mysite/public/"
  ServerName www.mysite.local
</VirtualHost>
Shakil Alam
  • 308
  • 4
  • 9