<p>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 <code>p</code>:</p>

<pre><code>&lt;a href="members/login?p={{ craft.request.path }}"&gt;Log in&lt;/a&gt;
</code></pre>

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

<p>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 <code>redirect</code> input:</p>

<pre><code>&lt;input type="hidden" name="redirect" value="{{ craft.request.getQuery('p')|default('/') }}" /&gt;
</code></pre>

<p><em>Note that <code>craft.request</code> should be all lowercase; in Craft almost all variables or service names follow the <code>camelCase</code> naming convention (the <code>craft.request</code> variable refers to the <a href="https://craftcms.com/classreference/variables/HttpRequestVariable"><code>HttpRequestVariable</code></a> class, which wraps the <a href="https://craftcms.com/classreference/services/HttpRequestService"><code>HttpRequestService</code></a>).</em></p>

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

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

<pre><code>{% set redirectUrl = craft.request.getQuery('p') %}
{% if redirectUrl %}
    {% redirect redirectUrl %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-02-01T13:55:31.900Z
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.
Adam Menczykowski
CC BY-SA 3.0
2016-02-01T13:57:18.973Z
Or even more succinctly... `<input type="hidden" name="redirect" value="{{ craft.request.getUrlReferrer()|default('/') }}" />`
Adam Menczykowski
CC BY-SA 3.0
2016-02-01T14:05:45.047Z
...or `value="{{ craft.request.getQuery('p')|default(craft.request.getUrlReferrer()|default('/')) }}"` :P
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-02-01T08:25:55.607Z
2016-02-01T08:25:55.607Z
Mats Mikkel Rummelhoff
<p>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.</p>

<p>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.</p>

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

<p>For example I thought that:</p>

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

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

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

<p>All help greatly appreciated, thank you!</p>

<redirect><login>
Store a redirect URI for use after login
# Store a redirect URI for use after login

(Asked by: username_0 on 2016-02-01)

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 
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!

**Tags:** redirect, login


---

## 1 answers

### Answer by username_1 on 2016-02-01. Score: 11 | Accepted answer

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:
Log in

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:


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 %}

> **Comments:**
> **username_0**: 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.  
> **username_0**: Or even more succinctly... `<input type="hidden" name="redirect" value="{{ craft.request.getUrlReferrer()|default('/') }}" />`  
> **username_1**: ...or `value="{{ craft.request.getQuery('p')|default(craft.request.getUrlReferrer()|default('/')) }}"` :P  

---

