I have some pages that require login from the user. What I'd like is for the user to get redirected to the page that they wanted after they've logged in rather than sent to the page that they were on before they attempted to view a 'login required' page.

At present I have this in my login form:

<input type="hidden" name="redirect" value="{{ craft.request.getUrlReferrer }}">

Is there a craft request method I can use instead of getUrlReferrer to accomplish this?

1 Answer 1


If you are using {% requireLogin %} tags, then the Craft UserSession service has a getReturnUrl() method which should give you what you want. It is accessible in your templates via craft.session.returnUrl. Here is a link to the docs. Hope this helps!

  • 2
    I'm trying to do this same thing with Craft 3 and not sure how exactly. I simply have a link (below) on a page outside of the CP admin but want to redirect users back to this page after they've logged in on the normal CP admin login page. So a way to temporarily override craft.app.user.returnUrl if they're coming from this external page to the main CP login form. ``` <a href="{{ cpUrl() }}" class="font-bold text-blue underline">Click here to log in</a> ``` Apr 5, 2019 at 20:13
  • 1
    Ok so I came up on this solution which will work in my case. It would be cool to somehow pass a temporary returnUrl it with a link to the login form so the user understands what's going on. At any rate I added {% requireLogin %} to my external page's template and then just needed (in my case) to add 'loginPath' => 'cp-admin/login' to general.php as well. docs.craftcms.com/v3/dev/tags/requirelogin.html#app --- or doy I can just a put a login form on the page itself by tweaking this one. docs.craftcms.com/v3/dev/examples/login-form.html Apr 5, 2019 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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