Hello Geek, There are two ways to remove public
from the URL in Laravel 10.
Method 1: Using .htaccess
- Create a
.htaccess
file in the root directory of your Laravel application. - 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>
- Save the
.htaccess
file.
Method 2: Using server.php
- Create a
server.php
file in the root directory of your Laravel application. - 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';
- 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!