How to Remove public folder from the URL in Laravel

In this post I am going to tell you how can we remove public folder which comes in the URL of Laravel website when it is used to upload on web hosting.To remove this folder, there are 2 ways.1st one is to update some code into .htaccess file of hosting so following is the code which needs to be updated into .htaccess file.

Solution 1

.htaccess File

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
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
# Use PHP7 as default
AddHandler application/x-httpd-php73 .php

Solution 2

By Editing index.php file

After uploading the files into root directory of your web hosting goto public folder under root directory and move index.php file to root directory and edit the following code of line.

if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
    require $maintenance;

require __DIR__.'/../vendor/autoload.php';

You need to remove /.. from above lines of code and after removing these symbol, it will look like following.

if (file_exists($maintenance = __DIR__.'/storage/framework/maintenance.php')) {
    require $maintenance;

require __DIR__.'/vendor/autoload.php';

So this was about to remove public folder from URL.I hope you got all which is explained by me.

Leave a Comment