The programmer feels boring with the URL related tasks. But here, we will show you the best ways to remove the public from the URL in Laravel. Let’s just dive into it.
Important Note: We have seen many times that the developer changes the Laravel system files like server.php to index.php & changes the path in files etc. Please don’t do that. That is not a proper way. Do you think that Laravel makers are foolish? We don’t think so. Never ever try to touch any Laravel system file.
01 Remove Public From URL On Your Live Server
In this case, you just need to set the document root to the public folder path and you’re done. Please check the below screenshot.
02 Remove Public From URL In Localhost Server By Creating Virtual Host
By using Virtual Host, We can host our local project in our local server by using domain names like example.local, demo.local, myproject.local, etc.
Here, We are showing you an example of the particular Windows operating system. The following steps may vary as per the operating system.
- Go to
C:\Windows\system32\drivers\etc\
open the “hosts” file in Administrator mode. - Let’s say, we want to create a Virtual Host for the domain “example.local”. You can specify any as you like. Just make it constant at every place. Add the following code into the “hosts” file and save it.
127.0.0.1 example.local
- Now go to,
C:\xampp\apache\conf\extra
for Xampp users and for the Wamp user “C:\wamp\bin\apache\Apache2.4.4\conf\extra
” and open “httpd-vhosts.conf” file. Now add the following code into it.Notes: Change the
"DocumentRoot"
as per your project path and also add a domain name as you define into the"hosts"
file.
<VirtualHost example.local>
ServerAdmin example.local
DocumentRoot "C:/xampp/htdocs/example"
ServerName example.local
ErrorLog "logs/example.local.log"
CustomLog "logs/example.local.log" common
</VirtualHost>
- Last but the most important step is to restart your Xampp or Wamp. Then try to access the URL like
http://example.local
and your Laravel project should respond without a public in URL.
03 Remove Public From URL Via One Command In Laravel
If you are working with the localhost then you don’t need to do anything more. You just need to run the following command from your terminal or command-line tool.
The php artisan serve
command is used to serve the application on the PHP development server.
> php artisan serve
After running the above command terminal shows you a message something like this "Laravel development server started: http://127.0.0.1:8000"
. So Laravel hosted itself on that local URL. Try to access the URL http://127.0.0.1:8000
in the browser. You can see it’s working right.
How to Share Local Laravel Project to Other People in LAN?
Sometimes, People wanted to share project URL to other people working in LAN. In such a case, You need to run your project on your own IP.
To get your own IP, open the command-line tool and run "ipconfig"
(for Windows user). It will list out the different IPs but we need only IPv4 address. We are preassuming that your IPv4 is 192.168.0.177
. Now, run the below command.
> php artisan serve --host=192.168.0.177
That’s it. Go to the web browser and try to access your web application on 192.168.0.177
Do you stuck with the port issue? Don’t worry, you can even change the PORT. You just need to run the below command and you’re done.
> php artisan serve --host=192.168.0.177 --port=77
04 Remove Public From URL Using htaccess In Laravel
Usually, This way is very useful for shared hosting. You need to create a .htaccess file in your project’s root directory and write the below code into it. Try to access the URL and check public will be removed now.
<ifmodule mod_rewrite.c>
<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>
That’s all from our end. We hope this article helped you to learn the best way to remove the public in Laravel in the very simplest way.
Additionally, read our guide:
- Specified Key Was Too Long Error In Laravel
- Run PHP Artisan Commands On Shared Hosting Servers
- How To Calculate Age From Birthdate
- Active Directory Using LDAP in PHP or Laravel
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Difference Between Factory And Seeders In Laravel
- Difference Between Events and Observers In Laravel
- Session Not Working In Laravel
- Laravel 8: Target Class Controller Does Not Exist
Please let us know in the comments if everything worked as expected, your issues or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post ๐
Very nice to read and follow again. Hope to read more about this. Brittani Teddy Nanji
Glad that you like the post. Thanks ๐
You made some nice points there. I looked on the internet for the subject matter and found most individuals will agree with your site.
appropriate
If some one wishes expert view concerning running a blog after that
i propose him/her to go to see this blog, Keep up the good job.
What a information of un-ambiguity and preserveness of valuable experience regarding unexpected feelings. Mari Kristoforo Valdas
Excellent post. I am dealing with some of these issues as well.. Kittie Duky Tchao
Hi to every one, the contents existing at this website are actually awesome for people knowledge, well, keep up the nice work fellows. Illa Angel Herb
I cannot thank you enough for the post. Thanks Again. Great. Cicily Cyrill Alaric
I appreciate, result in I found just what I was having a look for. Elisa Patrizius Danielle
Hi to all, the contents existing at this web site are truly remarkable for people experience, well, keep up the nice work fellows. Annie Flem Nella
Hi, this is the best I’ve found so far about this incredibly difficult subject in Laravel. I took the .htaccess way and it partially works but when i start from localhost/myapp it will redirect me to localhost/myapp/public/index.php but somehow it won’t *remove* the public or index.php part (so I get stuff like myapp/public/index.php/customers). This is laravel 8.x and I’m keeping the .htaccess in /public but the same thing happens if i delete it. Any ideas? Thanks!
You need to create new .htaccess file into the root directory, there is no need to touch the default files of Laravel. If you are working locally then you should create a virtual host, which is the best and easy way.
I correct myself: if I delete /public/.htaccess I get a total error, so that must stay but something is missing to remove the public/index.php bit..
#5 worked for me. I created .htaccess file. Thanks.
This is a very informative article. Thanks for sharing these helpful articles.
Regards,
Web Tecky