Last updated September 13, 2012. Created by fago on August 22, 2008.
Edited by rvanderh1, Francewhoa. Log in to edit this page.

The first short tutorial will show, how rules module can be used, to configure a simple page redirection after a user has logged in.

Notes

  • This tutorial is for the Rules module. Not Trigger core module. Trigger module's tutorial can be found here.
  • As an alternative to the following tutorial, one can just import the ready rule from here.

Requirements

Optional

  • Token contributed module if you want to use tokenized URL. Make sure that the Token sub-module Token actions is activated.

Steps

  1. Create a new rule

    First we have to start a new rule. For this go into the rules admin center, click on "Triggered rules" and on the tab "Add a new rule". Select the event "User has logged in", set the label "On login redirect to account page" and click "Submit".
    Now we have created a new rule, which is still empty. Every condition and action that we add to this rule, will be evaluated on the event "User has logged in".

  2. Create the redirection action

    To do so, click on the "Add an action" link, under the section "System", select the action "Page redirect" and click on "Next" button. In case you have the token module installed, the "Page redirect" action allows you to use token replacements. This allows us to place the user's id in the redirect path.
    So call the action "Redirect to the users account page" and set the path user/[account:uid]. Then activate the checkbox "Force redirecting to the given path, even if a destination parameter is given" - which is necessary, because Drupal always sets a destination parameter to the current page, if a user logs in. So, that our redirection applies, we need to activate this. Check the option, and click on the "Save" button.

    That's all. Just log out and log in again - you'll be redirected... :)

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

FYI following this tutorial, you'll log in and get a page not found error if you don't install the token module. The url trailer might end "/user/[account%3Auid]" upon logging in without enabling that module.

I tried to use this tutorial to change the login redirection from the user account (default in my Drupal installation) to the front page.

This tutorial din't work for me at all. As with so many instructions here the people writing them never take the time to put the correct terms and links in to make life easy and to avoid confusion.

Anyway, may be this tutorial works and I am on the wrong "garden path". In any case this is what I did to get users on login to the front page.

1. ..../admin/settings/actions (Actions settings page)
Go to the bottom and open the drop down menu to the left of [create] and choose Redirect to URL press [create].
2. A new window opens - "Configure an advanced action"
Here you should read -
Description:
Redirect to URL
In the line below add the page link where your users should arrive at after logging in. In my case I choose <front>.
URL:
<front>
Press [Save] and now you should have a new line added to "Actions available to Drupal:"
system ....... Redirect to URL ..... configure .... delete
3. Next step - go to Triggers. You can click on it above in the explanation text or use ..../admin/build/trigger/node.
Now choose the user tab or .../admin/build/trigger/user.
Go down to Trigger: After a user has logged in.
From the drop down box choose "Redirect to URL" and press [Assign].

Done!!! [I hope ;-) ]

If you are redirecting to another page you might need to experiment with the link code along the lines of .../content/user-start-here-page.

Good luck

-----------
Good luck .....
... more recent results of trying Drupal just once are -
www.native-power.de

This really works thanks!

I had no luck with rules redirecting to frontpage but the action and trigger combo really solve it.

I know that this is probably idiotic, but it took me too long to realize that I had to enable the Trigger Module. I thought I would drop a note here to save other people like me 20 minutes.

idiotic but useful, this simple tip saved me a lot of headaches. Thanks!!

Thanks you.

Thanks for your comment snohio. I have updated the tutorial by adding a 'Requirements' section.

---
How to contribute to Drupal.

Your comment saved my day today. Great thanks! :-)

BUT... I have two sub-sites - each of them I would like to redirect to its own front page after logging in. With one sub-site your procedure works smoothly, but the second sub-site is always redirected to /admin.

Moreover I cannot force neither of them to redirect to any other URL (e.g. http://drupal.org, or node/2, or ... ... ... ...). It seems that there is only one direction for each sub-site (somewhere written) and nothing can chage it.

Is there any problem with my configuration? I am quite new in Drupal world... Maybe any module I need to install or switch on.

I appreciate any help.

For redirecting to the front page just follow the tutorial at the top of the page and leave the redirect field empty. Explanation, since the redirect is relative to the sites Base URL (domain) leaving the redirect field empty will redirect to the base url. Anything placed in the redirect field will just be appended to the base URL.

Thanks tryitonce. I re-wrote the tutorial base on you comment.

---
How to contribute to Drupal.

Hi Onopoc,
The original tutorial worked for me using the Rules module. I suggest you place your re-write into documentation for the trigger module, NOT the rules module. I think a lot of people are confused that trigger and rules modules are alternative, distinct implementations.

Adding my support for this. This tutorial is in the wrong place. I came looking for a Rules solution, not the Trigger and action modules

Quentin Campbell
Digital Solace (Pty) Ltd

Note that the trigger and rules modules are separate, alternative solutions to the task of "triggering actions". It appears that you were unable to get the rules tutorial to work, so you developed your own similar procedure using the trigger module.

I'm also still on the learning curve on this, but I think the rules module claims to be a more versatile and powerful alternative to the trigger module. The documentation doesn't make it completely clear, but I think one should turn off the trigger module in order to use rules.

This works but it doesn't use the Rules module it uses the triggers module. This tutorial is for a different module the rules module. This comment should be moved to the triggers module.

sorry for the redundancy here. I didn't see the above comment in the stacking order.

Mike

You need to install latest development version for token replacements to work. Any version after 27-AUG-2008. http://groups.drupal.org/node/10270/rules-modules
Also see FAQ here http://groups.drupal.org/node/15542

I know I'm a newb, but I have followed the main instructions and the one in the thread exactly and cannot get the redirect to actually work. Assuming there is no step missing in either walkthrough, has anyone else had a problem getting this to work? I'm so excited about learning and using rules, but hitting a roadblock right away, am I the only one?

EDIT

I was able to setup an exact same rule on users logging out. So my problem seems to be specific to a rule when a user logs in. Leaves me even more confused onto why... but at least I'm getting rules to work. Anyone have an explanation for this?

Hi,

I set an action (redirect URL[internal or external links) to the Trigger (After a user has logged in). When a user login, it always goes to the orginal page (the page when it is logout status).

I tried to set another action (display message) to the same trigger (after a user has logged in). It shows the message.

Can anyone tells me why this URL redirect does not work?

Note: I tried both action/trigger or rules module. The results are the same.

Thanks.

hosais.

I've the same problem: the debug message is displayed, but it shows the destination token, not the token value, and it doesn't do the redirect.

same problem(

I was getting same problem but now got the solution. In newer version of rules there will be a option of "force destination or redirection" just check that option and redirection would work.

I had the same problem until I set the destination value to 〈front〉

Totally agree with jsulmar. In a tutorial about Rules to give examples of how to do something with Triggers and Actions - NOT Rules! - is ridiculous and confusing! For the sake of future readers please remove the Trigger and Actions example and put a Rules example.

GlossyIbis

Well, while you have a point it might still be good to show how things can be done one way or another.

What would help is a page that explains the difference of Rules and Triggers & Actions to those searching for solutions.

Much in in Open Source environment is confusing as there is no "Big Brother" structure. On the other hand there is a type of organised chaos and it actually seems the be the quickest way to find a solution.

Just watch how ants are searching what they need to bring back for the community. They have a random non-systematical approach and research has shown that this is the quickest and most efficient way of searching. So, having Rules and Triggers & Actions mixed up will eventually lead to finding something more quickly and should help people to understand the difference as experts like you add their experience to the melting pot.

Thanks ....

-----------
Good luck .....
... more recent results of trying Drupal just once are -
www.native-power.de

Gonna have to agree here. Came to find a basic example to guide me into the realm of Rules because the other tutorials are a step ahead of me at the moment... and this tutorial doesn't even have to do with Rules.

Welp way to get my hopes up.

Indeed this tutorial was listed under the wrong category. I moved it to Trigger module's book at http://drupal.org/handbook/modules/trigger

Direct link: http://drupal.org/node/789066

Any volunteer to write a tutorial for the rules module?

---
How to contribute to Drupal.

Try custom destination module.

http://drupal.org/project/customdestination

Here is a key thing to know that may not be so obvious: when doing a preg_match you have to cast it as a bool. For example, I want to assign a role at login based on what the LDAP-authenticated user has as their DN. Just checking against the $account->data (which has to be unserialized as well) with preg_* won't work without the proper data type. This is what I had to do in the custom PHP code for the conditional:

$ldap = unserialize($account->data);
$pattern = "/.+dc\=student\,dc\=university\,dc\=edu/i";
$retval = (bool)preg_match($pattern, $ldap['ldap_dn'], $matches, PREG_OFFSET_CAPTURE);
return $retval;

If "dc=student,dc=university,dc=edu" is in the user's DN, the student role gets assigned to them.

Modules used:
ldap_integration-6.x-1.0-beta2
rules-6.x-1.3

Andrew Sledge
Developer - Novologic

@asledge

Doesn't ldapgroups handle the role assignment stuff for you? Are you using custom PHP code to work around a problem of some kind?

The reason I ask is I'm not getting a role assigned on first login via ldapgroups. On second login, the group is correctly assigned. So, for example, if I have studentA sign in via LDAP for the very first time (i.e. her account is just now created via ldapauth), she doesn't get assigned the *student* role prior to rules executing. But if I log out and log back in, she already has the student role, and a rule looking for that role works perfectly.

Craig

At the time, yes: we had other things (besides what you saw) going on that required us to get a definitive boolean response.

Andrew Sledge
Developer - Novologic

This is going to sound so lame. Make sure the path you specify actually exists - I had created a page called volunteers but set the redirect to volunteer. Spot the difference, obvious once you know.

How can I use this tutorial to redirect user after the registration to a page where there is a welcome text