13.11.2025 | Pascal Crott

Prevent the user profile from being displayed in backend theme using RouteSubscriber

RouteSubscriber.php
<?php

namespace Drupal\scp_base\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    // Make sure profile pages are not admin pages, in order to load the right
    // translation.
    if ($route = $collection->get('profile.user_page.single')) {
      $route->setOption('_admin_route', FALSE);
    }
  }

}

Issue:

Authenticated users should never see the admin/backend theme in your websystem

Prerequisites:

You need to have the profile module installed in your project.

Solution:

Paste the provided code snippet into your RouteSubscriber.php file.