How to remove /public/ from URL in Laravel

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:

Remove Public Laravel

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';