How to Remove Public From URL in Laravel

Remove public from url in laravel; In this tutorial, we will learn how to remove the public path from URL in laravel 11, 10, 9, and 8 apps using .htaccess and server.php file.

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

There are two ways to remove the public\index.php path from URL in laravel 7, 8, 9, 10 apps; is as follows:

Solution 1 – Using.htaccess file

To create and update the .htaccess file in the Laravel. You can find .htaccess file in root directory.

You must have mod_rewrite enable on your Apache server. The rewrite module is required to apply these settings. You also have enabled .htaccess in Apache virtual host for Laravel.

Update the code into your .htaccess file:

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

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

Follow the below steps to remove public from url in laravel:

  1. The first step is to rename server.php to index.php in Laravel project.
  2. Copy the .htaccess file from /public directory to your Laravel root folder.

If you couldn’t find server.php in your Laravel project. So, you need to create two files named Index.php and .htaccess in your laravel project root directory.

Firstly create .htaccess file into laravel project root directory and add the following code to it:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Next, you need to create Index.php in the laravel project root directory and add the following code to it:

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

$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';

Conclusion

In this tutorial remove the public from URL in laravel. Here you have learned two ways to remove the public from URL in laravel.

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *