Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

in this article, I'll show you how to redirect user after  login in drupal 8 & 9.

Create file


name: Redirect User After Login
description: customize url redirect after user logged in successfully.
package: Custom
type: module
core: 8.x
core_version_requirement: ^8 || ^9

configure: redirect_user_after_login.settings


then enable this module:

redirect user after login


Create file


  title: 'Login Redirect'
  parent: system.admin_config_system
  route_name: redirect_user_after_login.settings


Create redirect_user_after_login.routing.yml file


  path: '/admin/config/system/login_redirect'
    _form: '\Drupal\redirect_user_after_login\Form\RedirectUserAfterLoginForm'
    _title: 'Set Login Redirection Url'
    _permission: 'access content'


Create src/Form/RedirectUserAfterLoginForm.php file



namespace Drupal\redirect_user_after_login\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class RedirectUserAfterLoginForm extends ConfigFormBase

     * {@inheritdoc}
    public function getFormId()
        return 'redirect_user_after_login_form';

     * {@inheritdoc}
    public function buildForm(array $form, FormStateInterface $form_state)
        // Form constructor.
        $form = parent::buildForm($form, $form_state);
        // Default settings.
        $config = $this->config('redirect_user_after_login.settings');

        $form['url_after_login'] = array(
            '#type' => 'textfield',
            '#title' => $this->t('Url Redirection'),
            '#required' => TRUE,
            '#default_value' => $config->get('redirect_user_after_login.path'),
            '#description' => $this->t('Add a valid url or &ltfront> for front page'),

        return $form;

     * {@inheritdoc}
    public function validateForm(array &$form, FormStateInterface $form_state) {
        $path = $form_state->getValue('url_after_login');
        $is_valid = \Drupal::service('path.validator')->isValid($path);
        if ($is_valid == NULL) {
            $form_state->setErrorByName('url_after_login', $this->t('Path not found.'));

     * {@inheritdoc}
    public function submitForm(array &$form, FormStateInterface $form_state) {
        $config = $this->config('redirect_user_after_login.settings');
        $config->set('redirect_user_after_login.path', $form_state->getValue('url_after_login'));
        return parent::submitForm($form, $form_state);

     * Gets the configuration names that will be editable.
     * @return array
     *   An array of configuration object names that are editable if called in
     *   conjunction with the trait's config() method.
    protected function getEditableConfigNames()
        return [


then you can set your login redirection url

Redirect after user login in drupal 8


Create redirect_user_after_login.module file


To redirect user after  login in drupal 8 & 9, You need to alter the login form and add a submit handler which will take care of the redirection using RedirectResponse.


use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;

 * Implements hook_form_alter().
function redirect_user_after_login_form_alter(&$form, FormStateInterface $form_state, $form_id)
    switch ($form_id) {
        case 'user_login_form':
            $form['#submit'][] = '_login_page_redirect_user_login_form_submit';

 * Custom submit handler for login form.
function _login_page_redirect_user_login_form_submit($form, FormStateInterface $form_state)
    $path = \Drupal::config('redirect_user_after_login.settings')->get('redirect_user_after_login.path');
    if ($path == '<front>') {
        $path = '/';
    $homeResponse = new RedirectResponse(URL::fromUserInput($path)->toString());



Next steps

  • Clear your Drupal 8 caches. To do this I use this Drush command: drush cr if you don’t currently use Drush, I highly recommend using it, or the Drupal Console.
  • I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.





Submitted by m.zerres (not verified) on Wed, 06/23/2021 - 10:25 Permalink

Thanks for the usefull code. There is an issue with D9.2. The login doesn't work no more when the module ist activated. I didn't have tested on a previous version. Failure: RuntimeException: Failed to start the session because headers have already been sent by "/***/vendor/symfony/http-foundation/Response.php" at line 368. in Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start() (Zeile 152 in /***/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php) Any idea?
Submitted by Ashish (not verified) on Thu, 11/04/2021 - 12:40 Permalink

Best module to redirect the user after login & logout is This module facilitates administrator to set the redirection in case user has multiple roles. Thanks Vipin MIttal
Add new comment

Restricted HTML

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

I am a senior web developer, I have experience in planning and developing large scale dynamic web applications especially in Drupal and Laravel.

Web Posts


Page Facebook