I need to pass the current page's URI into either the HTTP headers or as a URL query string so that after being taken to the login page and successfully logging in, the user will be sent back to where they came from.

I think that by attaching a query to any login button around the site I can then tell the login page where to go. The Login button is part of many of my templates / partials show when conditionally displaying content depending on if user is logged in.

By using <a href="members/login?p={% craft.request.path %}">

For example I thought that:

{% if craft.request.getQuery('p') and craft.request.getQuery('p')|length %}
    {% set p = craft.request('p') %}
{% else %}
    {% set p = '/' %}
{% endif %}

would set me up to redirect if the url contains http://craft.fira/members/login?p=/members/directory/json. However that results in a 404. Is there a safe way to pass a path into a url query?

Secondly, I then need to know how to deal with this after logging in successfully.

All help greatly appreciated, thank you!

1 Answer 1


First – your Twig syntax is a little off. Here's how you could add the current URL/request path to a link, as a query parameter p:

<a href="members/login?p={{ craft.request.path }}">Log in</a>

Notice that the syntax/tag for printing a value/variable is {{ value }}, not {% value %}.

Second, since the redirect in this case should happen when the login form is successfully submitted, it makes sense to add the redirect URL to the form as a redirect input:

<input type="hidden" name="redirect" value="{{ craft.request.getQuery('p')|default('/') }}" />

Note that craft.request should be all lowercase; in Craft almost all variables or service names follow the camelCase naming convention (the craft.request variable refers to the HttpRequestVariable class, which wraps the HttpRequestService).

Additionally, the above example demonstrates use of the default filter, which will print the declared value (in this case, /) if the value preceding the default statement is null or false. In this case, using default will save you an if statements and several lines of code.

Finally, if you want to do a (conditional) redirect outside of a form submission, you can make use of Twig's redirect tag:

{% set redirectUrl = craft.request.getQuery('p') %}
{% if redirectUrl %}
    {% redirect redirectUrl %}
{% endif %}
  • 2
    I'd like to add that there is another way of doing this without the URL query. Simply using: {% set redirect = craft.request.getUrlReferrer() %} <input type="hidden" name="redirect" value="{{ redirect }}" /> on the login page will get the URL referrer (the previous page that linked to the login page). Thus making the process work when a content editor links to the login page manually without the URL query. Feb 1, 2016 at 13:55
  • 1
    Or even more succinctly... <input type="hidden" name="redirect" value="{{ craft.request.getUrlReferrer()|default('/') }}" /> Feb 1, 2016 at 13:57
  • 1
    ...or value="{{ craft.request.getQuery('p')|default(craft.request.getUrlReferrer()|default('/')) }}" :P Feb 1, 2016 at 14:05

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.