Remove public from url in laravel

When you working with laravel you always found that you have to either run command or use localhost/project_name/public to run the project.

But what if you want to run the project without public folder?

So, Today i’m going to tell you the steps that helps you to remove public from your route in laravel framework.

Hope you have installed fresh laravel.

you have to follow steps listed below:

  • Copy index.php and .htaccess file which are placed in public folder.
  • Paste that file in root folder.

now just make some changes in your index.php file following code.

require DIR.’/../vendor/autoload.php’; // Old line
$app = require_once DIR.’/../bootstrap/app.php’; // Old line

require DIR.’/vendor/autoload.php’; // New line
$app = require_once DIR.’/bootstrap/app.php’; // New line

Replace the old line code with the new line in project root index.php file

I have already uploaded index.php and .htaccess both file for your reference. so you can also download hat files and placed that in your project root folder. and your project will run without public folder.

Now check your site by removing PUBLIC folder from URL.

Once you made the above listed changes you will find that your site is working fine without adding public in URL but now your assets(Like css,Js,Images,etc…) are not loaded properly as they are not loaded from public folder.

So for that what you have to do is, you may have to add your all assets with from public directory path in URL, Like i have added here in my blade file.

<link href=”{{ asset(‘public/asset/css/bootstrap.css’) }}” rel=”stylesheet” type=”text/css” />

add file in your blade template like above example.
you have to make sure while adding css,js or images always use asset() so this will works in localhost as well as on your server.

So this will take assets from public folder and you have not include public in URL everywhere in system.

You can find the index.php and .htaccess files from here.

Please like and share to needful.
Hope this helps you and reduce your time to google for the problem. 🙂 🙂 😀

Leave a Reply