How To Remove index.php From URL In Laravel 9

WebSolutionStuff | Jan-13-2023 | Categories : Laravel PHP

In this article, we will see how to remove index.php from the URL in laravel 9. Here, we will learn to remove the index.php from the URL in laravel 7, laravel 8, and laravel 9. The laravel application allows running like route_name/index.php/name. From an SEO perspective, this is not a valid URL.

So, let's see remove index.php from URL in laravel 7/8/9, how to remove index.php from the URL, laravel 9 remove index.php from URL.

If you want an index URL and use google search console. It can display duplicate URLs and also display errors like an alternate page with the proper canonical tags. So, we will remove index.php from the URL.


// Valid URL

// Invalid URL


There are multiple ways to remove index.php from the URL in laravel. 

  • Remove URL using the public/index.php file
  • Remove URL using RouteServiceProvider
  • Redirect with Nginx
  • Redirect with .htaccess


Example: Remove URL using the public/index.php file

In this example, we will simply add the below code to the public/index.php file.

if (strpos($_SERVER['REQUEST_URI'],'index.php') !== FALSE )
    $new_uri = preg_replace('#index\.php\/?#', '', $_SERVER['REQUEST_URI']);
    header('Location: '.$new_uri, TRUE, 301);


Example: Remove URL using RouteServiceProvider

In this example, we will remove/redirect the URL using the RouteServiceProvider.php file.


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 Illuminate\Support\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->routes(function () {
     * Configure the rate limiters for the application.
     * @return void
    protected function configureRateLimiting()
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());

    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);


Example: Redirect with Nginx

If you work with Nginx, then you can redirect the URL using the following code.

if ($request_uri ~* "^/index\.php(/?)(.*)") {
    return 301 $2;


Example: Redirect with .htaccess

In this example, you can add the below code to the .htaccess file and simply redirect to the main URL.

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect index.php URL
    RewriteRule ^index.php/(.+) /$1 [R=301,L]


You might also like:

Recommended Post

Featured Post

Follow us
facebooklogo github instagram twitter