Home ยป Best Way To Remove Public From URL In Laravel

Best Way To Remove Public From URL In Laravel

Last updated on January 4, 2021 by

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.

Table of Contents
1. Remove Public From URL On Your Live Server
2. Remove Public From URL In Localhost Server
3. Remove Public From URL Via One Command In Laravel
4. How To Share Local Laravel Project With Other People In LAN?
5. Remove Public From URL Using htaccess In Laravel

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.

remove public from url by setting domain root in laravel
Figure 1. Remove Public from URL on Your Live Server

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.

  1. Go to C:\Windows\system32\drivers\etc\ open the “hosts” file in Administrator mode.
  2. 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
  1. 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>
  1. 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:

  1. Specified Key Was Too Long Error In Laravel
  2. Run PHP Artisan Commands On Shared Hosting Servers
  3. How To Calculate Age From Birthdate
  4. Active Directory Using LDAP in PHP or Laravel
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Difference Between Events and Observers In Laravel
  10. Session Not Working In Laravel
  11. 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 ๐Ÿ™‚

 
 

11 thoughts on “Best Way To Remove Public From URL In Laravel”

  1. You made some nice points there. I looked on the internet for the subject matter and found most individuals will agree with your site.

    Reply
  2. 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.

    Reply
  3. What a information of un-ambiguity and preserveness of valuable experience regarding unexpected feelings. Mari Kristoforo Valdas

    Reply
  4. 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

    Reply
  5. 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

    Reply

Leave a Comment