How to do a redirect in node.html.twig? I'm trying to do a redirect on my node.html.twig template based on the content type because I'm using views for my content.

I tried this:

{% do redirect('/example') %}


or this:

{% redirect '/example' %}


but nothing works. Any idea?
Don't do it in the Twig file. This is not where that logic belongs. It should be in an Event.

See this similar question: EventSubscriber called on specific node type or path

All that is left is checking the node type and sending a RedirectResponse.

Gabe Sullice has a Gist that sounds very close to what you are trying to accomplish: https://gist.github.com/gabesullice/96bdae1518d11bb4565e72c33ffcae4c

So your code would fill in something like:

  /**
   * Determines whether we should send a RedirectResponse for this node.
   */
  protected function redirectApplies(NodeInterface $node) : bool {
    $redirect_types = ['type_1', 'type_2', 'type_3'];
    return in_array($node-&gt;getType(), $redirect_types);
  }


But if you go to different urls for different types, you will need to adjust it a little more.