Remove the Public from URL in Laravel

2019-10-07T07:32:26+00:00

Laravel is a very popular framework in PHP. But it comes with the public path in URL and this is one of the most common problems in the latest Laravel 5.* community. While set project on the local environment you face this problem. So, you need to resolve this problem before deploying your project on the server. Most of the time remove public from URL in Laravel problem can resolve by setting Document Root. But you can resolve by setting .htaccess file.

So what is the problem? It is not good when you have the public in your URL. So to remove this there are some methods and settings in configuration.

Methods to Remove public from URL

Using .htaccess file

Since you are already ready with Laravel project. Now, you can easily remove the public from URL in Laravel application. Follow the below steps to remove public from URL.

Rename Server.php to index.php

Firstly, you can rename server.php file to index.php file. You can find it in the root folder of your project.

Copy .htaccess file

Copy .htaccess file from public folder to root folder. Since this is the best option if you are working on the shared hosting. Change .htaccess files to below code or you can replace it.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
    RewriteRule ^\.env$ - [R=404,L,NC]
</IfModule>

 

However, this above .htaccess rule will resolve the issue regarding assets. Also, .env file access is also shown 404 error on browser. In other words, you don’t need to manually change any code for this condition. This rules will automatically handle those conditions.

Changes in Assets Path

So, you can see the public is removed from the URL. But due to this, there is one more problem occur in the applications. All the link and script assets will get 404 error. While getting those files you get “Not Found” error. So, to resolve this error you can simply prepend public in your assets URL. Therefore, you can resolve the “Not Found” error.

In this way, you can remove the public from URL using .htaccess rules. But make sure anyways you don’t give any unnecessary access to your application. If you find so then you can make changes in your .htaccess file.

Using Document Root

When you use a dedicated or private server for the web development then relatively simple to remove the public from URL. Now, you have access to the root. So, you can change any file on your server. But make sure you have ideas about what you are doing.

Open Virtual Host file of your server. and add the public to your Document Root.

1
2
3
4
5
6
7
8
<VirtualHost *:80>
    ServerAdmin info@example.com
    ServerName <a class="vglnk" href="http://example.com"  rel="nofollow"> <span>example</span><span>.</span><span>com</span></a>
    ServerAlias <a class="vglnk" href="http://www.example.com"  rel="nofollow"> <span>www</span><span>.</span><span>example</span><span>.</span><span>com</span></a>
    DocumentRoot {yoursourcedirectory}/public ErrorLog
    ${APACHE_LOG_DIR}/error.log CustomLog
    ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

 

In the above code, you will see the highlighted line. Well so, you can Simply add public in that line. Save the change in Virtual Host file and check your URL on the browser. However, you can restart your apache to reset the service. In this way, you can remove the public from the URL using the Document Root path.

Hence, you can use this method to remove the public from the URL in the Laravel application. In other words, you can use the .htaccess method on shared servers and Document Root method on a dedicated server. I hope this will help this post in your deployment. In short, comment below if you have any query regarding removal of public from URL.