How to remove public from URL in Laravel Framework

How to remove public from URL in Laravel Framework

You have just downloaded Laravel Framework via composer in your project folder, let’s say project folder name is myapp and you are trying to access through your browser. You have two ways to access it from your browser.

  • Access it through local/myapp
  • Access it through a virtual domain if virtual host is set up, say myapp.local

How to create virtual host in xampp in windows, mac and linux?

But when you type your address, all you get is the folder structure of Laravel, only way you can access your project home page is by typing localhost/myapp/public or myapp.local/public and you want to remove public from URL in your Laravel project.

Cause:

Public document folder is location defined by your web server where you can upload contents, such as web pages, images, javascripts, css etc.  Your webserver expects you to upload your website, applications and projects here as they are accessed by publics. A public folder can be named public_html, htdocs, www or simply public.

In the case of Laravel Framework, it expects you to set the public directory that it ships with as public document folder.

Solution:

There are many work arounds to take care of this. Here are some solutions.

1. The obvious solution.

The most obvious solution which would work in most of the cases, that involves moving few files and no coding is to install Laravel one folder above your server’s default document root.

Let’s say your default public document folder is set as htdocs, what you will do is install Laravel outside htdocs so that, the public folder from Laravel and the htdocs folder are in exact location. Now move everything from public folder to htdocs and magic! Everything will work.

Now what if you don’t want to do this? You know the obvious solution, now let’s check the preferred solution.

2. The preferred solution.

This is the preferred solutions which requires some editing to the file httpd-vhosts.conf located in apache configuration folder. Check this blog for full detail on file locations How to create virtual host in xampp in windows, mac and linux?.

Now if you are using localhost/myapp as your access url, use this

<VirtualHost *>
    ServerAdmin [email protected]
    DocumentRoot "C:/xampp/htdocs/YOUR_PROJECT_FOLDER/public"
    ServerName localhost
    ServerAlias localhost
    ErrorLog "logs/myapp-error.log"
    CustomLog "logs/myapp-custom.log" common
</VirtualHost>

If you are using a virtualhost like myapp.local , use this

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/xampp/htdocs/YOUR_PROJECT_FOLDER/public"
    ServerName myapp.local
    ServerAlias myapp.local
     ErrorLog "logs/myapp-error.log"
    CustomLog "logs/myapp-custom.log" common
 </VirtualHost>

What this does is sets public folder that was downloaded when installing Laravel as the default public document folder.

How to remove public from URL in Laravel Framework 1

2 Comments