# Redirect when it is in URL

**URL:** <https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441>\
**Category:** Need Help\
**Created:** [March 2, 2025, 9:13pm UTC](https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441 "2025-03-02T21:13:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![efjot](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@efjot](https://discourse.cakephp.org/u/efjot)\
**Post date:** [March 2, 2025, 9:13pm UTC](https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441/1 "2025-03-02T21:13:14Z")

</div>

I solved the topic to get redirected to login page when someone is trying to open a post directly without login.  
The user will be redirected to login page like this.

```auto
http://www.mypage.com/users/login?redirectUrl=%2Fposts%2Fview%2F8

```

But when user is logged in, then he should be automatically redirected to the page gibven in parameter redirectURL.  
How can I do this. Should this be implemented in login function?  
Thanks

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 2, 2025, 10:58pm UTC](https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441/2 "2025-03-02T22:58:09Z")

</div>

If somebody opens this URL (the login page with a redirect in it) directly when they are already logged in? Why would somebody do that? Where do you expect such a link to be, apart from generated exactly when they need it?

---

<div class="post-metadata">

**Author:** ![efjot](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@efjot](https://discourse.cakephp.org/u/efjot)\
**Post date:** [March 3, 2025, 8:47am UTC](https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441/3 "2025-03-03T08:47:33Z")

</div>

Maybe I explained it in a wrong way.  
This is the link I send in an email

```auto
http://www.mypage.com/posts/view/8

```

And when someone click on the link and got to this page, he will be automatically forwarded to the login page, because the user is not authenticated. So far so good.  
But after successfully login, then the user should come back to the page where the link was pointing on.  
And as you can see on my link before, the redirectUrl is already given in the URL, automatically.  
Bute for now, when I successfully logged in, I will come to the standard page and not to the redirectUrl in the parameter.  
How can I do this or should this be automatically?  
Thanks

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 3, 2025, 2:47pm UTC](https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441/4 "2025-03-03T14:47:30Z")

</div>

Does your login function look like the [provided example](https://book.cakephp.org/authentication/3/en/index.html#building-a-login-action), specifically the redirect when there is a valid result?

---

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [March 3, 2025, 11:22pm UTC](https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441/5 "2025-03-03T23:22:25Z")

</div>

the redirect is not done automatically. You need to tell it. See whether the `debug($redirect)` gives you the url. Output from debug should be like `/posts/view/8`

Assuming you are using **cakephp 5**

```auto
$redirect = $this->request->getQuery('redirect') ?? '/home' ; // Option 1. Redirects to 'home' if no referrer found
debug($redirect); // should output /posts/view/8
$redirect = $this->Authentication->getLoginRedirect() ?? '/home'; // Option 2. Redirects to 'home' if no referrer found
debug($redirect); // should output /posts/view/8
return $this->redirect($redirect);

```

---

<div class="post-metadata">

**Author:** ![dereuromark](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dereuromark/32/37_2.png) [@dereuromark](https://discourse.cakephp.org/u/dereuromark)\
**Post date:** [March 4, 2025, 1:20am UTC](https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441/6 "2025-03-04T01:20:34Z")

</div>

If you use the default auth way as documented, this would automatically be built in and be activated.  
The code should be sth like shown above using `$this->Authentication->getLoginRedirect()`.

---

<div class="post-metadata">

**Author:** ![efjot](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@efjot](https://discourse.cakephp.org/u/efjot)\
**Post date:** [March 9, 2025, 7:47pm UTC](https://discourse.cakephp.org/t/redirect-when-it-is-in-url/12441/7 "2025-03-09T19:47:02Z")

</div>

Thanks for your hint.  
I added the redirectURL in my login function.  
I check if redirectURL is not empty and redirect to this URL.
