n How to use Rules Module in drupal 8 to redirect user after logging in | CodimTh

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

Rules is a tool that enables you to define automatic, conditionally executed actions, triggered by various types of events. the basic outline of this functionality is:

  • WHEN an event happens
  • IF conditions are right
  • THEN take an action

Install Rules 

Run the following from the command line in the root of your project composer require drupal/rules 

Once this has run you can return to the Drupal dashboard and enable the Rules module.

Create the Rule

To create a rule navigate to /admin/config/workflow/rules then click on the Add a reaction rule button. Fill in the details with the following.

Label: Redirect user after loggin in

React on event: User has logged in


add reaction rule


Add the Conditions

Now we need to add a condition so click the Add condition button.

From the select box choose User has role(s) from under User and then click Continue.

When adding this condition it is useful to Switch to data selection for the Data selector input. The Data selector we want is @user.current_user_context:current_user

Leave the  Match roles as AND

Set the Roles value to authenticated. This is the machine name of "Authenticated user" role.

add user has role condition


We can now save the condition. if the following error is thrown: 

Error: Call to a member function id() on string 
in Drupal\rules\Plugin\Condition\UserHasRole->Drupal\rules\Plugin\Condition\
{closure}() (line 76 of modules/contrib/rules/src/Plugin/Condition/UserHasRole.php).

you will need to apply this patch: https://www.drupal.org/files/issues/2816157-10.patch, see this link https://www.drupal.org/patch/apply if you don't know how to apply a patch in drupal.

Add the Actions

We can now add an action to redirect Page after loggin in. Click the Add action button and choose Page redirect from the System section of the dropdown.
Enter the path (internal or external) to which the user should be redirected after logging in.

add page redirect action


We can now save the action.

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.
  • Now log in and check if the redirection is working.
  • I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.


Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

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