I need to redirect all the pages for a content type to a custom URL in Drupal 8.

I used to do this in Drupal 7 by implementing hook_init() with the function drupal_goto() or by using the Rules module. Unfortunately both hook_init() and drupal_goto() are removed in Drupal 8, and the Rules module is not stable yet, and I can't figure out how to do it in Drupal 8.

How can I achieve what I am trying to do?

  • 1
    You should look at the Change Records to learn about alternatives to removed functions/hooks. Specifically drupal.org/node/2013014 has an example extremely similar to what you are trying to do. – mradcliffe May 27 '16 at 1:47
  • Thanks @mradcliffe, actually I already tried to add Event Subscriber like in drupal.org/node/2013014 but the problem was in detecting the content type of the node in the node page. anyway, I managed to solve this issue and I added an answer :) – Aghiad May 27 '16 at 21:50

Here is how I managed to solve this issue, based on the example in drupal.org/node/2013014:

I registered Event Subscriber in my_redirect_module.services.yml file:

     class: Drupal\hbmsu_redirect\EventSubscriber\MyRedirectModuleSubscriber
       - {name: event_subscriber}

and then in the file my_redirect_module/src/EventSubscriber/MyRedirectModuleSubscriber.php

I added this code:

  namespace Drupal\my_redirect_module\EventSubscriber;

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

  class MyRedirectModuleSubscriber implements EventSubscriberInterface {

      public function checkForRedirection(GetResponseEvent $event) {

          $baseUrl = $event->getRequest()->getBaseUrl();
          $attr = $event->getRequest()->attributes;
          if(null !== $attr &&
             null !== $attr->get('node') &&
             $attr->get('node')->get('type')->getString() == 'my_content_type') {
              $event->setResponse(new RedirectResponse($baseUrl.'/some_internal_url_path'));

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


in checkForRedirection function, I'm checking if the page is for a node with the content type my_content_type, then redirect to the page mywebsite.com/some_internal_url_path


If you need to redirect from a custom form or an existing form (login, logout,...)=> (I assume this is what you want as you ask redirecting from a content type, I think you mean "after editing a content type");

then you can use


you can add this code in your form_alter, form_submit functions of your content type that you need to redirect

You can find some doc: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormSubmitterInterface.php/function/FormSubmitterInterface%3A%3AredirectForm/8.2.x

Like this example:

 array('node' => 123),
    'query' => array(
      'foo' => 'bar',
    'fragment' => 'baz',


  • Thanks @Baud, actually in my case I don't have form submission. I wanted to redirect all the node pages for certain content type to another page created with Views has a list of all these contents. anyway, I found the solution for this issue and I added an answer here :) – Aghiad May 27 '16 at 22:09

You can use Rabbit hole and Token modules. With Rabbit hole you will be able to set a Page Redirect for the content type, and with Token you can choose a field where users will be redirected when they try to access to a node.

  • If the nodes need to be secure, rabbit hole is the better option. It will block the node on more levels than what you could do with only using a Subscriber. – coderama Jan 5 '19 at 2:42

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.