Disable cache on base url for redirecting unauthenticated users I am trying to redirect unauthenticated users. So anytime an unauthenticated user tries to get to the base url, redirect them to an external url. Currently works, but continues to redirect to the external url even after user is authenticated and logged in. So I am trying to disable cache whenever the base URL is accessed. 

Here is my current code for redirecting in my src/EventSubscriber/RedirectAnonymousSubscriber.php file:

class RedirectAnonymousSubscriber implements EventSubscriberInterface {
  private $redirectCode = 301;

  public function checkForRedirection(GetResponseEvent $event) {
//    to kill page caching:
//    \Drupal::service('page_cache_kill_switch')-&gt;trigger();
    $is_authenticated = \Drupal::currentUser()-&gt;isAuthenticated();
    $route_name = \Drupal::routeMatch()-&gt;getRouteName();

    if (!$is_authenticated &amp;&amp; $route_name !== 'user.login') {
      $response = new RedirectResponse('https://example.com', $this-&gt;redirectCode);
      $response-&gt;send();
      exit(0);
    }
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkForRedirection', 30);
    return $events;
  }
}


Would that would be the correct way to go about things? And how would I go about implementing that in my *.routing.yml file and elsewhere?
Thanks!
Not Drupal specific, but you should use the status code 302. Redirects with a status code 301 are cached very aggressively by browsers.

In Drupal custom code, never send a response directly, set it in the event, so that it can be returned and cached properly by Drupal, which is by the way not a bad thing, because the page cache can handle different cached versions for authenticated and anonymous users:

$event-&gt;setResponse($response);