On access denied redirect to login page

Public

In a custom module

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_menu().
  3.  */
  4. function MYMODULE_menu() {
  5. $items = array();
  6.  
  7. // custom access denied menu item (redirects to login)
  8. $items['access-denied'] = array(
  9. 'page callback' => 'MYMODULE_access_denied',
  10. 'access callback' => TRUE,
  11. );
  12.  
  13. return $items;
  14. }
  15.  
  16. /**
  17.  * Page callback for the access denied page. Does a redirect to login page (Taken from r4032login.module)
  18.  */
  19. function MYMODULE_access_denied() {
  20. global $user;
  21. if ($user->uid) {
  22. return MENU_ACCESS_DENIED;
  23. }
  24.  
  25. // The code in drupal_get_destination() doesn't preserve any query string
  26. // on 403 pages, so reproduce the part we want here.
  27. $path = $_GET['destination'];
  28. $query = drupal_http_build_query(drupal_get_query_parameters(NULL, array('q', 'destination')));
  29. if ($query != '') {
  30. $path .= '?' . $query;
  31. }
  32. $destination = array('destination' => $path);
  33. header('Location: ' . url('user/login', array('query' => $destination, 'absolute' => TRUE)), TRUE, 302);
  34. drupal_exit();
  35. }

in settings.php

</> CopyGet raw version
php
  1. $conf['site_403'] = 'access-denied';