Remove “/index.php/” in Laravel URLs
Categorized as Laravel
If you’re dealing with duplicate URLs in you Laravel application, for example:
yoursite.com/contacts/
yoursite.com/index.php/contacts/
You can easily remove unnessary ones with permanent redirect. Here is the method which is proven across different hosting platforms. You don’t need to play around .htaccess
file or nginx cofiguration, just make the 301 permanent redirect with the RouteServiceProvider.php
.
Add this code to the app/Providers/RouteServiceProvider.php
protected function removeIndexFromUrl()
{
if (Str::contains(request()->getRequestUri(), '/index.php/')) {
$url = str_replace('index.php/', '', request()->getRequestUri());
if (strlen($url) > 0) {
header("Location: $url", true, 301);
exit;
}
}
}
And register the function in boot section:
public function boot()
{
...
$this->removeIndexFromUrl();
}
Now search engines will like your site.