Avoid public folder of laravel and open directly the root in web server
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
Sagiruddin Mondal
Researcher & 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 & CSS3 mysql
Updated on July 09, 2022Comments
-
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 likewww.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 !
-
MD Singh about 8 yearsyoutube.com/watch?v=ybJYyU5FPv4 Watch this :D
-
pankaj kumar over 3 yearsstackoverflow.com/questions/20547891/… use this link for better result.
-
pankaj over 2 yearsTry My Solution It will work everywhere. check Link stackoverflow.com/questions/25222509/…
-
-
Sagiruddin Mondal over 10 yearsThank you for your kind reply bro. I want to upload it on web server, in this case what should I do ?
-
Sagiruddin Mondal over 10 yearsBro I have done everything as per your instructions, It is showing 404 page instead . what should I do ?
-
lukaserat over 10 yearsI'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 over 10 yearsthank 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 almost 9 yearsThis worked for me. Just move the
.htaccess
andindex.php
files from the public directory into the root directory and change the bootstrap paths. -
Daniel Steiner about 8 yearsNo, 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 about 8 yearsYou 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 almost 8 yearsIs 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 almost 8 yearsCan you give solution for 5+ please , because I'm facing some difficulty with the latest version.
-
stef about 7 yearsMy osx development environment required a leading slash in front of "public/$1"
-
lukaserat about 7 yearsNot anymore @BakhtawarGIll Please see their documentation. Laravel changes their directory structure.
-
Yousef Altaf almost 7 yearsFor Laravel 5.2 just add this lines into
public/index.php
$app->bind('path.public', function() { return __DIR__; });
-
Puneet Verma about 5 yearssimplest solution. Thanks
-
MohcinBN almost 5 yearsThank you, your answer helped me a lot, i search for this answer two days
-
frankfurt-laravel over 4 yearsSame suggestion which Anik gave.
-
MItrajyoti over 3 yearsGood. A little more change removes the index.php too.
-
German Katz almost 3 yearsThanks man, this was the only .htaccess that make my project work!