Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-14T23:54:12.817Z
Answers.Text: 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!
Answers.UserDisplayName: John O
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-14T23:58:39.853Z
Answers.Text: Cool, no problem! :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-20T21:59:54.960Z
Answers.Text: 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.
Answers.UserDisplayName: Jordan Lev
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-14T17:27:26.593Z
Answers.LastActivityDate: 2015-10-14T17:27:26.593Z
Answers.UserDisplayName: Lindsey D
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-15T15:53:52.553Z
Answers.LastActivityDate: 2019-01-15T15:53:52.553Z
Answers.UserDisplayName: Luke
Body: <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>
  
Tags: <routes><controller><routing><redirect>
Title: Controller action redirection when called via route
ThreadText: # 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:
  
  ```html
  <input type="hidden" name="redirect" value="page/to/redirect/to"> 
  
  ```
  
  What you really want is this:
  
  ```php
  $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.
  
  
  ---
  
  