How to Remove Public from URL in Laravel 10

Hello Geek, There are two ways to remove public from the URL in Laravel 10.

Method 1: Using .htaccess

  1. Create a .htaccess file in the root directory of your Laravel application.
  2. Copy the following code into the .htaccess file:

Code snippet

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
  1. Save the .htaccess file.

Method 2: Using server.php

  1. Create a server.php file in the root directory of your Laravel application.
  2. Copy the following code into the server.php file:

Code snippet

<?php

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server.

if (preg_match('/\.php$/', $_SERVER['REQUEST_URI'])) {
    return false;
}

return require __DIR__ . '/public/index.php';
  1. Save the server.php file.

Once you have done either of these methods, you will no longer see the public directory in your Laravel application’s URL.

Here are some additional things to keep in mind:

  • If you are using a shared hosting provider, you may need to contact them to enable mod_rewrite.
  • If you are using a different web server than Apache, you will need to consult the documentation for that web server to find out how to configure it to remove the public directory from the URL.
  • If you have any custom routes that are defined in the public directory, you will need to update those routes to point to the new location of your application’s files.

All the best nerd!

Leave a Reply