Tags
By default in Laravel the URL to access your site is http://example.com/public/. It is not good to keep public in URL as it makes URL ugly and longer.
So, let’s talk about the solution to remove /public/ from the URL.
Method I : Using .htaccess
Create a .htaccess file your Laravel root directory if it does not exists already. (Normally it is under your public_html folder)
And add the following code to it:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule>
Now you can access your site via http://example.com.
Method II : Moving the contents of public folder to root directory
Actually i prefer this method because the above method is not so good to use in production environment.
So now lets make a new folder laravelfiles in your root directory and move all the files and folder except public directory to laravelfiles.
And move everything of public directory to root folder. Now your root directory will look something like this:
Now we have to change the paths in laravelfiles/bootstrap/paths.php file and index.php.
Find these lines in laravelfiles/bootstrap/paths.php
'app' => __DIR__.'/../app', 'public' => __DIR__.'/../public',
Change these two lines to:
'app' => __DIR__.'/../app', 'public' => __DIR__.'/../../',
Find these lines in index.php
require __DIR__.'/../bootstrap/autoload.php'; $app = require_once __DIR__.'/../bootstrap/start.php';
And change to:
require __DIR__.'/laravelfiles/bootstrap/autoload.php'; $app = require_once __DIR__.'/laravelfiles/bootstrap/start.php';
if my site is http://example.com/Fantasy/public/
how can I access my site via http://example.com/Fantasy/
I like Method I but is not work.
Agree with dontkillme, the .htaccess file does not work on my system either (I’m using MAMP, and my sites are under /Applications/MAMP/htdocs.
thank you it helps me a lot!
also found a good tut here http://tutsnare.com/remove-public-from-url-laravel/ looks both are best
I follow all the step, but It doesn’t work, i still need to put index.php in the url to call my controller.
how can i solve this? Thanks.
In Laravel 5.1 there is no file named path.php. So you have to do only second one. It works for me
Thanks ! it works perfectly.
Thank you for such a great post!
Thank you Sudeep! Exactly what I needed and works well on my hosting.
its not working for L5. This post is suitable for L4 only
thanks.. work for me..!!
Good writeup!