<p>You're mistaken about how <code>redirectToPostedUrl</code> works... That's designed to redirect from a form submission, when the <code>redirect</code> field was supplied:</p>

<pre class="lang-html prettyprint-override"><code>&lt;input type="hidden" name="redirect" value="page/to/redirect/to"&gt; 
</code></pre>

<p>What you really want is this:</p>

<pre class="lang-php prettyprint-override"><code>$this-&gt;redirect(craft()-&gt;request-&gt;getUrlReferrer());
</code></pre>

<p>The <code>request-&gt;getUrlReferrer</code> method will actually retrieve the originating page URL.</p>

CC BY-SA 3.0
2015-10-14T23:54:12.817Z
Yup, I knew redirectToPostedUrl wasn't what I needed, but just wasn't quite sure where to turn. This looks like exactly what I needed though, thank you!
John O
CC BY-SA 3.0
2015-10-14T23:58:39.853Z
Cool, no problem! :)
Lindsey D
CC BY-SA 3.0
2017-06-20T21:59:54.960Z
Note that `request->getUrlReferrer` actually uses the request headers from the browser, so you cannot assume that it exists and/or is reliable 100% of the time. To ensure you redirect back to the appropriate page you will need to include that hidden "redirect" input in your form.
Jordan Lev
CC BY-SA 3.0
2015-10-14T17:27:26.593Z
2015-10-14T17:27:26.593Z
Lindsey D
<p>In case you need this for CraftCMS version 3, as I did today, the code slightly changed:</p>

<pre><code>$this-&gt;redirect(Craft::$app-&gt;getRequest()-&gt;referrer);
</code></pre>

<p>This works fine with version 3.</p>

CC BY-SA 4.0
2019-01-15T15:53:52.553Z
2019-01-15T15:53:52.553Z
Luke
<p>I have a route set up in routes.php that calls a controller action using something similar to what is described here: <a href="https://straightupcraft.com/articles/how-do-i-setup-a-route-to-point-to-a-controller-action-instead-of-a-template" rel="nofollow">https://straightupcraft.com/articles/how-do-i-setup-a-route-to-point-to-a-controller-action-instead-of-a-template</a></p>

<p>This works much better for my purposes than POSTing a form to the controller. However, I'm not sure how best to handle redirects. Ideally I would like to redirect back to the page where the user clicked on the link, but calling</p>

<pre><code>$this-&gt;redirectToPostedUrl()
</code></pre>

<p>doesn't work when using a route like this. The URL just remains the URL as defined in the route.</p>

<p>Is there a different method I can use to make sure users are returned to the page where they were when they clicked the link?</p>

<routes><controller><routing><redirect>
Controller action redirection when called via route
# Controller action redirection when called via route

(Asked by: username_0 on 2015-10-14)

I have a route set up in routes.php that calls a controller action using something similar to what is described here: https://straightupcraft.com/articles/how-do-i-setup-a-route-to-point-to-a-controller-action-instead-of-a-template
This works much better for my purposes than POSTing a form to the controller. However, I'm not sure how best to handle redirects. Ideally I would like to redirect back to the page where the user clicked on the link, but calling
$this->redirectToPostedUrl()

doesn't work when using a route like this. The URL just remains the URL as defined in the route.
Is there a different method I can use to make sure users are returned to the page where they were when they clicked the link?

**Tags:** routes, controller, routing, redirect

---

## 2 answers

### Answer by username_1 on 2015-10-14. Score: 4 | Accepted answer

You're mistaken about how redirectToPostedUrl works... That's designed to redirect from a form submission, when the redirect field was supplied:
 

What you really want is this:
$this->redirect(craft()->request->getUrlReferrer());

The request->getUrlReferrer method will actually retrieve the originating page URL.

> **Comments:**
> **username_0**: Yup, I knew redirectToPostedUrl wasn't what I needed, but just wasn't quite sure where to turn. This looks like exactly what I needed though, thank you!  
> **username_1**: Cool, no problem! :)  
> **username_2**: Note that `request->getUrlReferrer` actually uses the request headers from the browser, so you cannot assume that it exists and/or is reliable 100% of the time. To ensure you redirect back to the appropriate page you will need to include that hidden "redirect" input in your form.  

---

### Answer by username_3 on 2019-01-15. Score: 3

In case you need this for CraftCMS version 3, as I did today, the code slightly changed:
$this->redirect(Craft::$app->getRequest()->referrer);

This works fine with version 3.


---

