Redirect basic page to external url when visiting page I'm attempting to provide a field to users to enable them to enter an external url. If they enter a url I would like that basic page to redirect to the url the user provided instead of actually opening the node. I've made some attempts by using a preprocess node hook in my custom .theme file as seen here:

function mytheme_preprocess_node(&amp;$variables){
    $node = Drupal::request()-&gt;attributes-&gt;get('node');
    $type = $node-&gt;getType();
    if($type == 'page'){
        $redirect = $node-&gt;get('field_redirect')-&gt;value;
        if($redirect != ''){

            $response = new Symfony\Component\HttpFoundation\RedirectResponse($redirect);
            $response-&gt;send();
            Drupal::entityManager()-&gt;getViewBuilder('node')-&gt;resetCache();
            return;
        }
    }
}


And I've also tried creating a module to do it and using an entity_view hook like so

&lt;?php

function mymodule_entity_view(array &amp;$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($entity-&gt;getEntityType()-&gt;id() == 'node' &amp;&amp; $entity-&gt;bundle() == 'page' &amp;&amp; !$entity-&gt;field_redirect-&gt;isEmpty()) {
    $value = $entity-&gt;field_redirect-&gt;getValue();
    $redirect = $value[0]['value'];
    $response = new Symfony\Component\HttpFoundation\RedirectResponse($redirect);
    $response-&gt;send();
    return;
  }
}


Each of these solutions work the first time, but don't work the 2nd time or any time after that until I clear the cache. I'm assuming this is a caching issue. I've heard that using cache tags could resolve this issue but I'm not sure how I would use them. Also I'm not married to making any of these solutions work I merely want to implement some way to perform redirects on pages, it doesn't matter to me how.

Is it possible to perform redirects on node pages?
this is a cache problem. You need to set correctly the cache tags and context for the entity and it will work.
You need to do something like this to skip the cache:

  '#cache' =&gt; ['max-age' =&gt; 0],


you can find more info here: https://www.drupal.org/developing/api/8/cache/tags

Notes:


This needs to be put on hook_entity_view_alter() on the entity in your case the node, or if you want in just one field for which you want to set it. This is a work around for your redirect issue!
Try to add it to the $entity, if it doesn't work you need to replace the default '#cache' or you can add it to the $build array, this is case dependent.