How to Remove Public from URL in Laravel 11?

05-Jun-2024

.

Admin

How to Remove Public from URL in Laravel 11?

Hi, Dev

In this guide, we will learn how to remove "public" from the URL in a Laravel 11 application using the .htaccess file.

When deploying a Laravel app on shared hosting, we often face the issue of the "public" folder appearing in the URL because we can't set the path to index.php directly. However, we prefer a cleaner URL without "public". In this guide, I will provide you with two solutions to remove "public" from the URL in a Laravel application.

I will suggest you don't have to use "shared hosting" for the laravel application and never go for this solution.

Solution 1: Laravel Remove Public From URL using .htaccess


In Laravel, the .htaccess file can be generated and edited to incorporate new changes. The .htaccess file is located in the root directory, and its modification requires mod_rewrite to be enabled on your Apache server. It is mandatory to enable the rewrite module to implement these changes. Moreover, you need to activate .htaccess in Apache virtual host to use it in Laravel.

Finally, Just go to root folder and create .htaccess file and upload with following code

.htaccess

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteRule ^(.*)$ public/$1 [L]

</IfModule>

Solution 2: Rename server.php and Move .htaccess File

To remove "public" from your Laravel URL, you need to complete the following two steps:

1. Rename the server.php file in your Laravel root folder to index.php.

2. Copy the .htaccess file located in the /public directory to your Laravel root folder.

3. All public folder css and js file move to root folder.

I hope it can help you...

#Laravel 11