-2
  • I have already indexed drupal-8 website's pages with pattern like - node?page=1; node?page=2 and so on
  • I would like to redirect them to site's front page

Tried:

  • I have tried with the redirect module but its not working
2

One way is to redirect page using events subscriber.

src/custom_module.services.yml

services:

    custom_module.custom_redirect:
        class: Drupal\custom_module\EventSubscriber\CustomRedirect
        tags:
          - {name: event_subscriber}

src/EventSubscriber/CustomRedirect.php

<?php

namespace Drupal\custom_module\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class to redirect.
 */
class CustomRedirect implements EventSubscriberInterface {

  /**
   * Check redirection.
   */
  public function Redirection(GetResponseEvent $event) {
    $attr = $event->getRequest()->attributes;
    $route_name = $event->getRequest()->get('_route');
   //Check route name and attr
    if ($route_name == 'check route name') {
        $front = \Drupal\Core\Url::fromRoute('<front>')->toString();
        $event->setResponse(new RedirectResponse($front));
      }

  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[KernelEvents::REQUEST][] = ['Redirection'];
    return $events;
  }

}
0

mytheme.theme

function mytheme_preprocess_page(&$variables) { // <-- your theme may already have one
  // redirect if URL has a "page" param.
  if (!empty(\Drupal::request()->query->get('page'))) {
    $front = \Drupal\Core\Url::fromRoute('<front>')->toString();
    $response = new \Symfony\Component\HttpFoundation\RedirectResponse($front);
    $response->send();
  }
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.