Drupal 8 and 9

Using custom code (source):

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_user_login_form_alter(&$form, FormStateInterface $form_state) {
  $form['#submit'][] = 'mymodule_user_login_submit';
}

/**
 * Form submission handler for user_login_form().
 *
 * Redirects the user to the dashboard after logging in.
 */
function mymodule_user_login_submit(&$form, FormStateInterface $form_state) {
  $url = Url::fromRoute('mymodule.dashboard');

  // Check if a destination was set, probably on an exception controller.
  // @see \Drupal\user\Form\UserLoginForm::submitForm()
  $request = \Drupal::service('request_stack')->getCurrentRequest();
  if (!$request->request->has('destination')) {
    $form_state->setRedirectUrl($url);
  }
  else {
    $request->query->set('destination', $request->request->get('destination'));
  }
}

Using a contributed module: See the Redirect After Login module.

Drupal 7

There are many ways to do this, either by using modules included in Drupal Core; or using any number of different contributed modules; or by building a custom module implementing the Drupal API. "Your mileage may vary" with each. Be warned, the login block sometimes behaves differently than the login page. Also, make sure you test the password reset path which can be affected.

Using Drupal core site-building

With Actions & Triggers

You'll first need to define the URL redirect action at admin/config/system/actions.

Then, after making sure the core module Trigger is enabled at /admin/modules (or using Drush), assign the URL redirect action you just defined to the "After a user has logged in" trigger at /admin/structure/trigger/user.

NOTE: This works from the /user page not any page within your site. It will not redirect from any other page. [&lt Needs clarification]

D7 Core - The exact steps to set your site to redirect your visitor 'after a user has logged in' using only Drupal 7 Core
  1. Establish what the node # is
    for the page that you want the user
    to be automatically redirected to
    after the user successfully logs in.

    To do that, go to the page
    that you want the user to be redirected to,
    and click the page-top choice
    "Edit".

    Then look in the address-bar of your browser,
    and note that at the end of the address is
    node/#SOME_NUMBER#/edit

    In my case, for example, I want to redirect
    the user to my Home page. When I 'edit' that page,
    the address ends in node/1/edit,
    which tells me that the page I want to redirect
    the user to is node # 1.

  2. Go to 'Administration' » 'Configuration' »
    and under the heading 'System', click "Actions".
    (Takes you to page: admin/config/system/actions)

  3. On the page 'Actions':
    See the Heading 'Create an advanced action',
    and click the downward pointing arrow
    at the right of 'Choose an advanced action'.

  4. Click the drop-down menu choice
    "Redirect to URL...".

  5. Click the button "Create".

  6. On the page 'Configure an advanced action',
    under the heading 'Label',
    you could leave the label as the default of
    "Redirect to URL".

    But, I want to distinguish this URL redirect
    from any other URL redirect I may make in the future,
    so I change the text-area box to:
    Redirect to URL - node #SOME NUMBER#

    In my case, for example, my label is now:
    Redirect to URL - node 1

  7. Under the heading 'URL', I type:
    node/1
    since that is where I want to redirect my user,
    though you will, of course, have to use the # associated
    with the page you want to direct your user to.

  8. Click the button "Save".

  9. You will now again be at the page 'Actions'.

    Note that your newly created action
    is listed in the table 'Available actions:'
    with an 'Action type' of 'system'.

    If you click 'configure',
    you can change the 'Label' or the 'URL'.

  10. Go to the 'Modules' page (admin/modules),
    and enable the 'Core' module 'Trigger',
    if it is not already enabled.

  11. Go to 'Administration' » 'Structure' »
    'Triggers', and click the tab "User".
    (Takes you to page: admin/structure/trigger/user)

  12. Under the heading
    'Trigger: After a user has logged in',
    click the downward pointing arrow.

  13. In the drop-down menu,
    click the name of the action you just created.

    In my case, for example, I click
    "Redirect to URL - node 1".

  14. Click the button "Assign".

    Note that under the heading
    'Trigger: After a user has logged in',
    your action is now displayed
    with the option to 'unassign' it.

  15. Logout, and log back in to test.

Using a contrib module

LoginToboggan

Download and install as usual: http://drupal.org/project/logintoboggan
Then go to settings page for the module and configure.
NB. Toboggan only gives you a custom redirect for newly registered users using immediate login. It does however have Rules integration.

Login Destination

Download and install as usual: http://drupal.org/project/login_destination
Then go to settings page for the module and configure.

Login Destination allows you to redirect onthe basis of evaluated php code (requires php filter.)

Login Redirect

A lightweight but relatively new solution which has support for non-internal redirects.
http://drupal.org/project/login_redirect

Using the Rules module

Download and install as usual: http://drupal.org/project/rules
This module is somewhat similar to Drupal core's Trigger module, but it is more advanced and offers more flexibility/configuration. The setup's configuration takes place with Rule's admin pages.

Comments

lmeurs’s picture

This seems to work for me on Drupal 7.12

function hook_user_login(&$edit, $account) {
  $edit['redirect'] = 'node/123';
}

Works for both regular login page and login forms loaded through drupal_get_form('user_login'). The user login block redirects to the page the user logged on to.

Laurens Meurs
wiedes.nl

leymannx’s picture

/**
 * Implements hook_user_login().
 */
function MYMODULE_user_login(&$edit, $account) {
	$edit['redirect'] = '<front>';
	// $edit['redirect'] = 'node/123';
	// works fine as well
}