CodeIgniter Forums
Redirecting does not remove anchor from the URL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Redirecting does not remove anchor from the URL (/showthread.php?tid=87427)



Redirecting does not remove anchor from the URL - Muzikant - 04-19-2023

If an anchor is in the old page, it is still presented after redirecting to the new page, where it does not make sense. An anchor should not be there until it is explicitly added in the redirection rule.



For example return redirect()->to('to'); redirecting http://localhost:8080/from#old-anchor to http://localhost:8080/to#old-anchor, which it should not.

The return redirect()->to('to#new-anchor'); redirecting http://localhost:8080/from to http://localhost:8080/to#new-anchor, which is how it should be.

It is also replacing an old anchor to a new anchor, which is also expected: return redirect()->to('to#new-anchor'); redirecting http://localhost:8080/from#old-anchor to http://localhost:8080/to#new-anchor.

So the only problem is in the first case, when old URL have an anchor, but the new URL does not. The same with the redirectResponse.



return redirect()->route('to'); and return redirect('to'); does not support anchors in the redirection rule, but the bug from the first example is presented. I tested it on a fresh CodeIgniter's 4.3.3 installation on PHP 8.1.12 on my Linux machine.


RE: Redirecting does not remove anchor from the URL - kenjis - 04-19-2023

Interesting. It seems the browser puts the fragment.

Workarount:
return redirect()->to('to', null, 'refresh');


RE: Redirecting does not remove anchor from the URL - Muzikant - 04-20-2023

(04-19-2023, 04:37 AM)kenjis Wrote: Workarount:
return redirect()->to('to', null, 'refresh');

Thank you, it works. I hope there will be a bug fix in the upcoming versions if it will be possible.


RE: Redirecting does not remove anchor from the URL - kenjis - 04-21-2023

This is not a bug. The browser keeps the fragment.
So we can't fix.


RE: Redirecting does not remove anchor from the URL - Muzikant - 04-23-2023

(04-21-2023, 05:12 AM)kenjis Wrote: This is not a bug. The browser keeps the fragment.
So we can't fix.

What about refreshing by default? If it is not a good way to solve it, then your solution could be mentioned in a documentation.


RE: Redirecting does not remove anchor from the URL - kenjis - 04-23-2023

(04-19-2023, 03:31 AM)Muzikant Wrote: return redirect()->to('to'); redirecting http://localhost:8080/from#old-anchor to http://localhost:8080/to#old-anchor, which it should not.

You say it should not. But why should not?
At least major browsers do so.


RE: Redirecting does not remove anchor from the URL - kenjis - 04-23-2023

(04-23-2023, 08:09 AM)Muzikant Wrote: What about refreshing by default?

It is a breaking change.

(04-23-2023, 08:09 AM)Muzikant Wrote: If it is not a good way to solve it, then your solution could be mentioned in a documentation.

No problem. Pull Request is welcome.
https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md


RE: Redirecting does not remove anchor from the URL - Muzikant - 04-24-2023

(04-23-2023, 05:30 PM)kenjis Wrote: You say it should not. But why should not?
At least major browsers do so.

There is a small chance, or at least not certain, that in the new location will be an old anchor. Maybe there is a reason why browsers doing it, but from my point of view it is not an expected behavior.



(04-23-2023, 05:32 PM)kenjis Wrote: No problem. Pull Request is welcome.
https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Thank you for inviting me, I will consider it. At the moment I am not registered at GitHub, because it is owned by Microsoft.


RE: Redirecting does not remove anchor from the URL - kenjis - 04-24-2023

Probably redirecting is expected for moving the URI with no content change.

Quote:URL redirection, also known as URL forwarding, is a technique to give more than one URL address to a page, a form, a whole website, or a web application.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections



RE: Redirecting does not remove anchor from the URL - ping-yee - 04-25-2023

Hello guys, I think add a notice in documentation is a good solution and I have been sent a new PR for this.
https://github.com/codeigniter4/CodeIgniter4/pull/7450