All beginners have misunderstood the Laravel routing and MVC structure and want to remove public from laravel url. The public clause is there for security purposes.
Whenever you try to run your application using your local server including Xampp or Wamp, you always encounter the public clause in your laravel URL.
Solution #1 – Best Way to Remove Public from Laravel URL
You can map your local IP or local web address to the public directory of Laravel.
- First, you need to add a local address to your host file.
- If on windows go to C:windows/system32/drivers/etc/hosts edit this file an administrator.
- add a new line at the end, 127.0.0.1 laravel.local
- save (as administrator).
Once done, then navigate to your Xampp or Wamp folder
- open, httpd-vhosts.conf file from xampp/apache/conf/extra directory
- Add the below lines to it and save it.
Options +Indexes +Includes +FollowSymLinks +MultiViews
3. Now restart your Apache server.
Now you can navigate to http://laravel.local in your browser and you won’t see any public clause in your url.
This is the only best way to remove public from laravel url.
Solution #2 On Live server (Shared Hosting or Cloud)
If you are running your Laravel project live on server using Shared Hosting or Cloud then you need to point the domain to public directory.
- Open your web hosting WHM, Cpanel or Cloud Account.
- Navigate to Add-ons or Domain on WHM and Cpanel or websites on Cloud.
- Manage or edit the domain listed for your laravel project.
- You may see the pointing directory something like username/public_html/laravel_website_domain/
- you need to change and point it to the public directory like this username/public_html/laravel_website_domain/public
- Once it is pointed to the public, you are good to go.
Also, read more about Specific table migration in laravel
Solution #3 Put .htaccess and index file in root (Not Recommended)
- Go to Laravel project
- navigate to public directory
- cut or copy the htaccess file and paste it in the root directory just besides .env and other files.
- rename your server.php file to index.php
- And you are good to go. (php artisan serve command won’t work anymore)
This is how to remove public from laravel url in the 3 different ways. Let me know in the comment section which one worked for you.
I personally do not recommend the third solution but sometimes it works for many people. It’s not recommended due to a security issue because it will make a loophole to let someone view the env file and you never want this to happen.