I've discovered an unwanted behaviour in one of my projects. This was not used to be so, Google indexed a url with index.php in it and gave a duplicate canonical notification.
It happend on a URL like this:
https://robindirksen.com/blog
https://robindirksen.com/index.php/blog
After I noticed it, I want to other known Laravel projects and discovered the same.
https://ohdear.app/feature/uptime-monitoring
https://ohdear.app/index.php/feature/uptime-monitoring
And also on the Laravel website self.
https://laravel.com/docs/6.x/validation
https://laravel.com/index.php/docs/6.x/validation
I contacted Mattias Geniar (from OhDear!) and he was really quick with a solution. The solution can be found on his personal blog, ma.ttias.be.
Personally I like this approach, because it's a known issue so it should be fixed in the framework itself and not on the server, like in nginx.
<?php
use Illuminate\Support\Str;
class RouteServiceProvider extends ServiceProvider
{
public function map(Router $router)
{
$this->removeIndexPhpFromUrl();
}
/**
* This will remove the index.php from the URL and prevent canonical conflicts.
*
* @return void
*/
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;
}
}
}
}