PHPLaravel

How to Remove Public from URL in Laravel? 2022

Laravel is the most popular PHP Framework in the world. If you are developing your web application with laravel, You have noticed when you upload your app into Cpanel or other local servers root directory public text appears in the URL. So you need to remove the public from the URL. There are two ways to remove public from URLs.

How to Remove Public from URL in Laravel?

The first way is to upload your laravel project on your server and set the public directory as a root directory. That’s it now you access your website without \public in the URL.

If you don’t have access to change your website root directory, You could follow the second way to remove /public from the URL. This method will work in all kinds of shared hosting. You just need to edit your project server.php and add some code to the .htaccess file. You can directly download these 2 files from GitHub and upload them into your project folder. Or You can do this manually by following these steps. Let’s start

Step 1: Rename server.php into index.php

Just go to your laravel project root directory and find server.php. After seeing the file, simply rename server.php into index.php. That’s it.

Step 2: Update .htaccess file in root directory

Now create a .htaccess file in your laravel project root directory and enter the following code into this .htaccess file.

Options -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
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]

After updating the .htaccess file, let’s run the project. Now you will see \public removed from the URL. I hope it works for you. If you find any issues, Please comment below.

Moin Uddin

Hi, I am Moin Uddin. A Web Developer from Bangladesh. I love to do coding and learn something new every day. You can contact me at [email protected]

Leave a Reply

Back to top button