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.
Example:
// Valid URL
https://valid-url-example.com/how-to-remove-index-php
// Invalid URL
https://valid-url-example.com/index.php/how-to-remove-index-php
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
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);
die();
}
In this example, we will remove/redirect the URL using the RouteServiceProvider.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 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->configureRateLimiting();
$this->removeIndexPHPFromURL();
$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'));
});
}
/**
* 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);
exit;
}
}
}
}
If you work with Nginx, then you can redirect the URL using the following code.
if ($request_uri ~* "^/index\.php(/?)(.*)") {
return 301 $2;
}
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]
</IfModule>
You might also like:
- Read Also: PHP 8.3: Release Date and New Features
- Read Also: 500 Internal Server Error In Laravel 9 AJAX
- Read Also: How To Fix cURL Error 60 SSL Certificate Problem
- Read Also: How To Solve The Page Expired 419 Error In Laravel