In Drupal 8 you use an event subscriber for this. You subscribe to a REQUEST kernel event, get the original url from the request, apply a regular expression and if this finds a replacement set the new url as a 301 redirect response.
mymodule/src/EventSubscriber/RedirectSubscriber.php
<?php
namespace Drupal\mymodule\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
class RedirectSubscriber implements EventSubscriberInterface {
public function redirectIndexPHP(GetResponseEvent $event) {
$old_url = $event->getRequest()->getUri();
$new_url = preg_replace('|/index.php|', '', $old_url, 1, $count);
if ($count > 0) {
$response = new TrustedRedirectResponse($new_url, 301);
$event->setResponse($response);
}
}
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('redirectIndexPHP', 39);
return $events;
}
}
mymodule/mymodule.services.yml
services:
mymodule.redirect:
class: Drupal\mymodule\EventSubscriber\RedirectSubscriber
arguments: []
tags:
- { name: event_subscriber }