Laravel is a very popular PHP framework and probably the best framework for developing PHP web applications. If you are not familiar with Laravel, please read this article.

We all know that a Laravel project can be run from localhost by visiting localhost:8000 from browser, if we write php artisan serve this command line. But when we export our project to a server, then we can not see our web application by visiting the domain name. Instead we see this page:

But if we go to the public folder by visiting www.yoursite.com/public/, we can easily see our web application. But obviously we don’t want this. We don’t want to send our visitor to the public folder instead of the main domain.

How to remove public from URL in Laravel

There are various ways to solve this. Some solution contains creating new .htaccess file and changing some rules. But for me, none of the solution worked perfectly.

Here I am sharing a solution which is very easy to do and it works on all Laravel 5 versions.

Step 1

Go to the public folder of your Laravel project and move everything from that folder to the project folder. So your project folder should look like this. And the public folder should be empty. So you can delete that if you want.

Step 2

Open index.php file from your project directory. In these file, you have to change two lines:

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

Change these two lines to these lines:

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

At the end, your index.php file should look like this. (Note: I’m using Laravel 5.2, some codes in the file may be different if you are using another version. But don’t worry. You have to change only those two lines)

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.com>
 */

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/

require __DIR__.'/bootstrap/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

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

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

Save the file and we are done!

Final Results

Now if we just visit the domain name without the public directory like www.yoursite.com, you will see your Laravel web application.

  1. First off I would like to say superb blog! I had a quick question which I’d
    like to ask if you don’t mind. I was interested to know how you center yourself and clear your mind prior to writing.
    I’ve had a tough time clearing my mind in getting my thoughts out there.
    I truly do take pleasure in writing however it just seems
    like the first 10 to 15 minutes are usually lost just trying to figure out how to begin. Any suggestions or tips?

    Thank you!

    • Hi Steve, thank you for reaching you. I’m glad that you liked my blog. I’m developing web applications using PHP for past 3 years and with Laravel Framework since last year.

      At the beginning, I faced several problems where I myself had to find the correct solutions. Sometimes even the correct answers from stackoverflow did not work for me. Now after one year, I will not say I am that expert but hopefully now I can develop with Laravel more confidently. So when I look back one year ago, I wish if I had known those solutions eariler and easily, it would been more helpful. From this thought, I started this website in order to help someone like me.

      Now about the tips of writing tutorial articles, firstly I try to put all my thoughts together in a new post. After writing everything down, then I try to read it from top to bottom and make some corrections. Then I re-arrange everything. Then I search on Google on the topic that I am writing. Then I take some suggestions from similar posts and try to implement these. Then I try to add some pictures that will be helpful for users. And lastly I publish the article.

      Please subscribe to my blog to receive updates on future posts. If you face any problems or need help, feel free to contact me. Have a great day!

  2. I love your blog.. very nice colors & theme. Did you make
    this website yourself or did you hire someone to do it for you?

    Plz respond as I’m looking to design my own blog and
    would like to know where u got this from. thanks a lot

  3. We are a bunch of volunteers and starting a new scheme
    in our community. Your web site offered us with helpful info to work on. You’ve done an impressive task and our entire group shall be thankful to
    you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>