# Retrieve the anchor from the called URL

**URL:** <https://discourse.cakephp.org/t/retrieve-the-anchor-from-the-called-url/10981>\
**Category:** Need Help\
**Created:** [February 24, 2023, 7:43am UTC](https://discourse.cakephp.org/t/retrieve-the-anchor-from-the-called-url/10981 "2023-02-24T07:43:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![electoonix](https://avatars.discourse-cdn.com/v4/letter/e/b5e925/32.png) [@electoonix](https://discourse.cakephp.org/u/electoonix)\
**Post date:** [February 24, 2023, 7:43am UTC](https://discourse.cakephp.org/t/retrieve-the-anchor-from-the-called-url/10981/1 "2023-02-24T07:43:27Z")

</div>

I need to redirect a user to a page but need to extract the anchor tag from the called url (to put it on the redirect url)  
I have try Router::getRequest()-\>getUri() but I don’t see the anchor tag in it.

What is the best way to extract the anchor tag from the called URL in cakePHP 4

thanks

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [February 24, 2023, 9:51am UTC](https://discourse.cakephp.org/t/retrieve-the-anchor-from-the-called-url/10981/2 "2023-02-24T09:51:48Z")

</div>

What you are probably looking for is the HTTP referer header which points to the original URL the user was on before he clicked on the anchor tag.

```auto
$referer = $this->getRequest()->referer();

```

---

<div class="post-metadata">

**Author:** ![electoonix](https://avatars.discourse-cdn.com/v4/letter/e/b5e925/32.png) [@electoonix](https://discourse.cakephp.org/u/electoonix)\
**Post date:** [February 24, 2023, 10:16am UTC](https://discourse.cakephp.org/t/retrieve-the-anchor-from-the-called-url/10981/3 "2023-02-24T10:16:14Z")

</div>

yes and no, in fact I have to add a GET parameter to that url, so I have to :  
1/ extract the anchor  
2/ add the parameter  
3/ re-add the anchor

adding the parameter after the anchor don’t work.

example : /mypage?param=1#anchor should become /mypage?param=1&param=2#anchor

also if the user come from google, It won’t work (but I am agree check that the host is mine is possible 🙂 )

thanks

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [February 24, 2023, 10:25am UTC](https://discourse.cakephp.org/t/retrieve-the-anchor-from-the-called-url/10981/4 "2023-02-24T10:25:41Z")

</div>

I would recommend you look at [authentication/AuthenticationService.php at 2.x · cakephp/authentication · GitHub](https://github.com/cakephp/authentication/blob/2.x/src/AuthenticationService.php#L365) which does basically the same.

You need to encode your desired URL as a GET parameter (via `urlencode()`) so whatever logic you have in your redirected page can extract that urlencoded data and `urldecode()` it.

You can’t just put a whole URL without any encoding inside another URL.
