How can i set this redirect only for anonymous users?
You can load a user and check for authentication

$currentUser = \Drupal::currentUser();

// Do nothing if the user is authenticated.
if ($currentUser-&gt;isAuthenticated()) {
  return;
}