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– monymirza2012-11-12 17:24:09 +00:00Commented 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– Aboodred12012-11-12 17:26:07 +00:00Commented 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.Boriana Ditcheva– Boriana Ditcheva2012-11-12 18:37:02 +00:00Commented 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– Aboodred12012-11-12 20:04:53 +00:00Commented Nov 12, 2012 at 20:04
- if anonymous users are not allowed to explore any site content, then remove permission to view contentmonymirza– monymirza2012-11-13 07:05:52 +00:00Commented Nov 13, 2012 at 7:05
5 Answers
The way I solved it as follow:
- Events: Drupal is initializing
Conditions:
- User has role(s) -
Parameter: User: [site:current-user], Roles: anonymous user - NOT Text comparison -
Parameter: Text: [site:current-page:url], Matching text: user/login
- User has role(s) -
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 7Aboodred1– Aboodred12016-09-26 20:27:09 +00:00Commented Sep 26, 2016 at 20:27
- i made an error and it is working.TopTomato– TopTomato2016-09-27 22:42:24 +00:00Commented Sep 27, 2016 at 22:42
- @TopTomato Awesome! Glad it worked for you.Aboodred1– Aboodred12016-09-27 22:47:26 +00:00Commented Sep 27, 2016 at 22:47
Go to /admin/people/permissions uncheck View published content for anonymous users

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

- 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– Aboodred12012-11-13 17:51:06 +00:00Commented Nov 13, 2012 at 17:51
- then remove 'user' from (access denied) page field at 2nd stepmonymirza– monymirza2012-11-14 04:36:23 +00:00Commented 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 moduleAboodred1– Aboodred12012-11-14 05:33:51 +00:00Commented Nov 14, 2012 at 5:33
- yes. further can be done by rules module. you can also do it via trigger core module.monymirza– monymirza2012-11-14 05:39:25 +00:00Commented Nov 14, 2012 at 5:39
- 1If someone has access denied does not mean they are not logged in.See This AnswerTanvir Ahmad– Tanvir Ahmad2017-01-03 07:40:42 +00:00Commented 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'){ drupal_goto('user/login'); } Or, if access to the user/registration & user/password pages is still wanted:
global $user; if ($user->uid == 0 && arg(0) != 'user'){ drupal_goto('user/login'); } - 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– Aboodred12012-11-14 05:37:19 +00:00Commented Nov 14, 2012 at 5:37
- @Chance, I'm curious. Where would this live within the .theme file?TikaL13– TikaL132018-09-12 19:42:24 +00:00Commented 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– Aboodred12012-11-12 22:59:21 +00:00Commented 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– Aboodred12014-04-14 00:53:39 +00:00Commented Apr 14, 2014 at 0:53