Laravel Remove index.php From URL

  • 12-10-2022
  • 1135
  • Laravel 9
  • Haresh Chauhan

In this post we will remove index.php from the URL, if you see the index.php file path in the URL when you open your laravel application and want to remove it, this post will help you to fix the index.php file remove from the URL.

index.php file generally you see when you call your controller method using route name. so having ended this article you can easy to fix this issue.

Also wants to remove the public from URL laravel using .htaccess, given that solution. Here we can do this thing two ways. The first method we can code in our provider file and the second method to fix this issue is solved by apache using the .htaccess file.

Removing index.php Using Laravel Code

Remove the index.php file name from the URL using the coding method. In this coding method, we add two methods in the route service to provide the file. We will add the map() method for the registration of our custom method. The second method we will create for redirecting if found index.php file.

app/Providers/RouteServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Str;

class RouteServiceProvider extends ServiceProvider
{
    /**
        * The path to the "home" route for your application.
        *
        * This is used by Laravel authentication to redirect users after login.
        *
        * @var string
        */
    public const HOME = '/home';

    /**
        * Define your route model bindings, pattern filters, etc.
        *
        * @return void
        */
    public function boot()
    {
        $this->removeIndexPhpFromUrl();
        
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('web')
                ->group(base_path('routes/web.php'));

            Route::prefix('api')
                ->middleware('api')
                ->group(base_path('routes/api.php'));
        });
    }

    protected function removeIndexPhpFromUrl()
    {
        if (Str::contains(request()->getRequestUri(), '/index.php/')) {

            $url = str_replace('index.php/', '', request()->getRequestUri());

            if (strlen($url) > 0) {
                
                header("Location: $url", true, 301);
                exit;
            }
        }
    }

    /**
        * Configure the rate limiters for the application.
        *
        * @return void
        */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60);
        });
    }
}

Redirecting index.php In .htaccess

If you want to choose the .htaccess file. copy the below-given code and paste it into your application .htaccess file. Using the .htaccess file if there is found index file in the URL, it will redirect to the non-index file URL itself.

<IfModule mod_rewrite.c>
     RewriteEngine On

     # Redirect if index.php is in the URL
     RewriteRule ^index.php/(.+) /$1 [R=301,L]
</IfModule>

Default index.php Root Path Change And Remove Public From The Url

[issue] Possible All Way To Change Asset Path And Add Public To Asset Path In Laravel


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :