In this tutorial you will learn about the Remove index.php from the URL in Laravel and its application with practical example.
In this article, I’ll show you how to remove index.php from the URL in your laravel application. if you notice in your laravel application you have to access links like:
1 |
route/index.php/login |
In this tutorial, i will share a solution with you to remove index.php from URL of your laravel application.
Remove index.php from the URL in Laravel
if you want remove index.php from URL in laravel application then just open following file and make the changes below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
<?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 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->removeIndexPHPFromURL(); $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); }); } /** * Write code on Method * * @return response() */ 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)->by(optional($request->user())->id ?: $request->ip()); }); } } |
Create Route
Now, open your route file and add following route to it.
routes/web.php
1 2 3 4 5 |
Route::get('/contact-us', function () { dd('Contact Us'); }); |
now if you open url like bellow then:
1 |
https://example.com/index.php/contact-us |
will redirect to:
1 |
https://example.com/contact-us |