• 9849-xxx-xxx
  • noreply@example.com
  • Tyagal, Patan, Lalitpur
Help
How to remove Public from URL in Laravel? [updated @ Mar 10, 2021]

How to remove Public from URL in Laravel? [updated @ Mar 10, 2021]

Laravel framework is awesome to use. But for beginners, it might be irritating to fix the issue where user needs to goto /public file to access newly created project. If that is your case, well, you are at right place.

Update @ Mar 10, 2021

Simple approach to redirect from domain.com/public to domain.com is using .htaccess edit. If you don’t have .htaccess in your root direction of your website, create it one by:

  • Type nano .htaccess from your terminal or if you have cpanel/directadminpanl or any admin panel, create a file with a name .htaccess.
  • Now paste the following code.
  • Press control+x
  • Type y
  • Press enter
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Above solution works perfectly if you have your laravel project in root folder. Are you searching for .htaccess code to remove public from sub-folder laravel projects? Well, again you are at the right place ?. Follow all the steps above and just replace your .htaccess code with the following code.

<IfModule mod_rewrite.c>
 #Session timeout

<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

</IfModule>

Once you save above .htaccess code, you will be able to remove public from your laravel project.

Below you will another approach too which I don’t prefer but it works. The reason I don’t prefer renaming approach is because it breaks git system. It is not recommended to alter file or code which are managed from git.

Old post

The process is very simple with only 2 steps. Here are the steps.

  1. Goto to your laravel root folder of your project and you will find server.php file.
  2. Rename server.php to index.php.
  3. Now goto /public folder and find .htaccess file. If you don’t find .htaccess file, it means you have turned off show hidden file setting in your computer. Here is steps on how to show hidden files.
    1. Mac users:
      1. Press Command+Shift+. and you will be able to see all hidden files.
      2. And if you want to hide again, Please the same keys and you are done. So easy.
    2. Windows users:
      1. Under View menu, check Hidden items And you are good to go.
  4. Copy the .htaccess file from /public folder to your Laravel root folder.
  5. Now open your website localhost:8888/your_project. Isn’t is working now? I hope so.

This way, you can enable accessing your website directly from your root folder instead of typing /public which doesn’t even look better for the website. I hope once day Laravel team will do the changes within its framework without need to do anything at first place. But till that time, well here is the solution for you guys.

I hope, this helped you. If so, don’t forget to share and it would be highly appreciated if you give a like for this post. Let me know if you have problem. I will be happy to help you.

Thanks

Leave a Reply

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