You could use the Rules module for this. Here is the rule you'd need for it (in Rules export format, just use copy/paste to import it in your own setup):
{ "rules_redirect_admin_path_to_user_path" : {
"LABEL" : "Redirect admin path to user path",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "init" : [] },
"IF" : [
{ "user_has_role" : {
"account" : [ "site:current-user" ],
"roles" : { "value" : { "1" : "1" } }
}
},
{ "text_matches" : {
"text" : [ "site:current-page:path" ],
"match" : "admin",
"operation" : "starts"
}
}
],
"DO" : [
{ "drupal_message" : { "message" : "To access this path (= /admin) you need to login first, you will be redirected automatically to path (= /user)." } },
{ "redirect" : { "url" : user" } }
]
}
}
This rule uses 2 Rules Conditions:
- Is this an anonymous user?
- Does the current path start with
/admin
?
Whenever such redirect actually happens (that is: both Rules Conditions are satisfied), the user will also receive an informational message like so:
To access this path (= /admin
) you need to login first, you will be redirected automatically to path (= /user
).
If you don't want such message to appear, then just delete the line containing drupal_message
in the exported rule above.
Similar to the previous answers, it might require you to enable an extra contributed module (Rules). But, as indicated by its growing popularity also, that module is probably already enabled in mostly any site (similar to the Views module), because there are dozens of use-cases for this module).
Note: IMO it makes sense to perform such redirect for any path that starts with /admin
. But should you rather prefer to perform such redirect only for an exact match for path /admin
(instead of starting with such path), then simply replace the 2nd Rules Condition with this one (that is: omit the "operation" which is "starts"):
{ "text_matches" : {
"text" : [ "site:current-page:path" ],
"match" : "admin"
}
}