How to remove public folder in url in laravel 10, 11 and Above versions

Share this

How to Remove “public” from URLs

Laravel developers often face an issue where the public folder appears in their URLs. By default, Laravel serves the application through the public directory, but for clean and professional URLs, it’s essential to remove it.

In this guide, we’ll explore how to properly remove the public folder from Laravel URLs.

Method 1: Using .htaccess File

A simple way to resolve this issue is by modifying the .htaccess file in the root directory of your Laravel project. Follow these steps:

  1. Create a new .htaccess file in the root directory of your Laravel project (if it doesn’t exist already).
  2. Add the following code to the file:
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php
</IfModule>

Create server.php code in root directory of Laravel project

<?php

/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/

$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}

require_once __DIR__.'/public/index.php';

Share this

Leave a Comment