Laravel Remove Public\index.php from URL using htaccess

Laravel Remove Public\index.php from URL using htaccess

To remove public /index.php from url in laravel; Simply copy .htaccess file from public folder to laravel project root folder and create new index.php file in root folder.

In this tutorial guide, you will find very easy two options that help you remove “public\index.php” from your Laravel project or app.

How to Remove “Public\Index.php” From Laravel URL Using htaccess

Here are two options to remove public index.php from url using htaccess file in laravel applications:

Option 1 – Create index.php and Copy .htaccess file

Here are steps to remove public index.php from url in laravel using htaccess:

  1. The first step is to create index.php in Laravel root folder.
  2. Copy the .htaccess file from /public folder to Laravel root folder.

Let’s start to removing public and index.php from url:

Go to your Laravel project root folder, and create Index.php in it, then add the following code to index.php:

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

Now, copy .htaccess file from public folder to laravel root folder, and update the following code into 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>

Option 2 – Enable Rewrite Mode with .htaccess

Firstly, open your cmd or terminal window and run sudo a2enmod rewrite to enable rewrite mode on your Apache server; command as follow:

sudo a2enmod rewrite

Once you have enabled rewrite mode, Now you need to do some configuration in .htaccess file. Simply create .htaccess file in laravel root folder and add the following code into it:

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

Restart your Apache web server to apply the changes by running the sudo systemctl restart apache2 command:

sudo systemctl restart apache2 

Conclusion

That’s it; you have learned two different ways how to remove public index.php from url in laravel projects.

Recommended Laravel Tutorials

AuthorAdmin

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 *