I'm using rules module with drupal 7 in order to redirect anonymous users to the login page, so I created a rule with react on event "Drupal is initializing", condition "User has role" with anonymous user and "Not site login url", then added an action "Page redirect" to site login url. When I tried to test the rule; drupal kept redirecting to the login page over over and over until apache server stopped working. Any recommendations.

  • can anonymous user explore site content? for what reason need to redirect login page?
    – monymirza
    Commented Nov 12, 2012 at 17:24
  • anonymous users are not allowed to explore any site content and not allowed to register except with an invitation. Users have to login to use the site.
    – Aboodred1
    Commented Nov 12, 2012 at 17:26
  • I don't understand this part "rule drupal kept redirecting to the login page without showing it." Could you explain what you mean? Otherwise I'm not sure I understand which part isn't working. Commented Nov 12, 2012 at 18:37
  • Well when I go to the homepage or any other page but not login page the rule works fine; its redirecting the user to the login page, however the browser keeps redirecting to login page over and over until apache stops working.
    – Aboodred1
    Commented Nov 12, 2012 at 20:04
  • if anonymous users are not allowed to explore any site content, then remove permission to view content
    – monymirza
    Commented Nov 13, 2012 at 7:05

5 Answers 5


The way I solved it as follow:

  • Events: Drupal is initializing
  • Conditions:

    1. User has role(s) - Parameter: User: [site:current-user], Roles: anonymous user
    2. NOT Text comparison - Parameter: Text: [site:current-page:url], Matching text: user/login
  • Actions: Page redirect - Parameter: URL: user/login

Export rule as follow:

{ "rules_login" : {
    "LABEL" : "login",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "login" ],
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "1" : "1" } }
      { "NOT text_matches" : { "text" : [ "site:current-page:url" ], "match" : "user\/login" } }
    "DO" : [ { "redirect" : { "url" : "user\/login" } } ]
  • @TopTomato this is for drupal 7
    – Aboodred1
    Commented Sep 26, 2016 at 20:27
  • i made an error and it is working.
    – TopTomato
    Commented Sep 27, 2016 at 22:42
  • @TopTomato Awesome! Glad it worked for you.
    – Aboodred1
    Commented Sep 27, 2016 at 22:47

Go to /admin/people/permissions uncheck View published content for anonymous users

enter image description here

now go to /admin/config/system/site-information add 'user' in (access denied) page field

enter image description here

  • good work around, but I still need to show access denied page because not all users have the same permissions. I appreciate your help.
    – Aboodred1
    Commented Nov 13, 2012 at 17:51
  • then remove 'user' from (access denied) page field at 2nd step
    – monymirza
    Commented Nov 14, 2012 at 4:36
  • if I remove it; anonymous users can only see access denied without redirecting to the login page. I'm sure that there's a way I can do it in rules module
    – Aboodred1
    Commented Nov 14, 2012 at 5:33
  • yes. further can be done by rules module. you can also do it via trigger core module.
    – monymirza
    Commented Nov 14, 2012 at 5:39
  • 1
    If someone has access denied does not mean they are not logged in.See This Answer Commented Jan 3, 2017 at 7:40

I'm sure you can pull this off with rules, but it would be easy to do in your theme's template.php file as well.

Something like this should work:

global $user;
if ($user->uid == 0 && arg(0) != 'user' && arg(1) != 'login'){

Or, if access to the user/registration & user/password pages is still wanted:

global $user;
if ($user->uid == 0 && arg(0) != 'user'){
  • Thanks @Chance I already found this solution before I posted my question. I want to use rules module or at least understand why its not working with "Drupal is initializing"
    – Aboodred1
    Commented Nov 14, 2012 at 5:37
  • @Chance, I'm curious. Where would this live within the .theme file?
    – TikaL13
    Commented Sep 12, 2018 at 19:42

The problem here is the event in question. Instead of Drupal is initializing, try Content is viewed.

  • I tried it before I post this question and it worked fine with me except with view pages, registration page, basically any page that is not node.
    – Aboodred1
    Commented Nov 12, 2012 at 22:59

I know this is a post from two years ago. However, the solution for redirecting an anonymous user to the user login page with rules is as follows:

  • event: System log entry is created
  • condition: User has role(s): anonymous
  • action: page redirect value: user

Hope this will help someone.

  • have you tried your solution before you post it here?
    – Aboodred1
    Commented Apr 14, 2014 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.