How to Remove Public From URL in Laravel 10

Spread the love

Hello Guys,

In this comprehensive guide, we’ll explore the effective method of utilizing server.php and .htaccess files to remove Public segment from the URL structure within Laravel projects spanning across versions 10, 9, 8, and 7. This tutorial will provide you with valuable insights into optimizing your Laravel project’s URL structure.

How to Remove Public\Index.php From URL in Laravel 10, 9, 8, 7
Examples of Remove Public From URL:

In Laravel versions 7, 8, 9, and 10, you have two methods to eliminate the ‘public/index.php‘ path from your URL.

Also Read: How to Integrate Stripe Payment Gateway In Laravel 10

  • Solution 1: Utilizing the .htaccess File
  • Solution 2: Renaming server.php and Relocating the .htaccess File

Solution 1 – Using .htaccess file

to create and modify the Laravel .htaccess file. The .htaccess file is located in the root directory.

Mod_rewrite must be enabled on your Apache server. To use these options, the rewrite module must be installed. Additionally, you’ve enabled .htaccess for Laravel in the Apache virtual host.

The code in your .htaccess file has to be updated:

<IfModule mod_rewrite.c>
   RewriteEngine On 
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Solution 2 – Rename server.php and move .htaccess file

For Laravel, use the steps following to remove the public from the URL.

  • The initial step is to rename server.php to index.php in your Laravel root folder.
  • /public directory’s .htaccess the file should be copied to your Laravel root folder.

We trust you’ll discover our Laravel Blogs to be a valuable resource.

Leave a Comment