![How to remove Public from URL in Laravel? [updated @ Mar 10, 2021] How to remove Public from URL in Laravel? [updated @ Mar 10, 2021]](https://cdn.statically.io/img/santoshm.com.np/wp-content/uploads/2019/08/laravel-remove-url.png?quality=100&f=auto)
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.
- Goto to your laravel root folder of your project and you will find
server.php
file. - Rename
server.php
toindex.php
. - Now goto
/public
folder and find.htaccess
file. If you don’t find.htaccess
file, it means you have turned offshow hidden file
setting in your computer. Here is steps on how to show hidden files.- Mac users:
- Press
Command+Shift+.
and you will be able to see all hidden files. - And if you want to hide again, Please the same keys and you are done. So easy.
- Press
- Windows users:
- Under
View
menu, checkHidden items
And you are good to go.
- Under
- Mac users:
- Copy the
.htaccess
file from/public
folder to your Laravel root folder. - 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