I want to redirect every users page to front page? like if user page url is /user/97 then it redirect to front page if user is not administrator. I want user page inaccessible to other users but only administrators.

How can I achieve this?

I tried doing like this:


namespace Drupal\myisu_ubit\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
// use Drupal\user\Entity\User;

   * Listens to the dynamic route events.
 class RouteSubscriber extends RouteSubscriberBase {

  * {@inheritdoc}
 protected function alterRoutes(RouteCollection $collection) {
   // Change the route associated with the user profile page (/user, /user/{uid}).
   if ($route = $collection->get('user.page')) {



     class: Drupal\module\Routing\RouteSubscriber
        - { name: event_subscriber }

What am I doing wrong here?

  • Pretty easy to do with code, there is also Rules, and a handful of contributed modules that provide this. – Kevin Nov 9 '18 at 20:59
  • How to?please show me. I tried with controllers and routes but unsuccessful also I don't wanna use Rules – tasqyn Nov 9 '18 at 21:02
  • Why do you need controllers and routes? The most basic level only needs to use hook_form_alter. drupal.stackexchange.com/a/223452/57 you can look at the logged in user and inspect their roles, then do the redirect. – Kevin Nov 9 '18 at 21:04
  • 1
    Possible duplicate of Redirect after user login – Kevin Nov 9 '18 at 21:04
  • For some reason I read this as "after logging in". – Kevin Nov 9 '18 at 21:06

Solved using EventSubscriber like this:

    class: Drupal\my_module\EventSubscriber\MyModuleSubscriber
       - {name: event_subscriber}

And MyModuleSubscriber:

namespace Drupal\my_module\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\views\Views;
use Drupal\views\ViewExecutable;
use Drupal\user\Entity\User;
use Drupal\Core\Url;

class MyModuleSubscriber implements EventSubscriberInterface {

   * {@inheritdoc}
  public function checkForRedirection(GetResponseEvent $event) {
    $request = \Drupal::request();
    $requestUrl = $request->server->get('REQUEST_URI', NULL);
    $userID = \Drupal::currentUser()->id();
    $user = User::load($userID);
    $uid = $user->get('uid')->value;
    $roles = $user->getRoles();
    $userPage = "/user/" . $userID;
    if (
      $userID !== '1' && 
      !in_array('administrator', $roles) &&
      $requestUrl == $userPage
    ) {
        $path =  \Drupal::service('path.alias_manager')->getAliasByPath('/');
      $event->setResponse(new RedirectResponse($path, 301));

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


I don't know if it is the correct way to do this but it solves my problem.

  • This is the correct way to redirect, in general. But in this case, to make pages inaccessible, the Drupal way would be to use permissions. Then you can redirect on the resulting 403 exception to show the frontpage instead. – 4k4 Nov 11 '18 at 23:12

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.