Home ยป Best Way to Remove Public from URL in Laravel

Best Way to Remove Public from URL in Laravel

Last updated on May 26, 2020 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 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 file 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 a very simplest way.

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 ๐Ÿ™‚

 
 

Leave a Comment