Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove a specific part of a link whilst redirecting in PHP?

I was looking at other places and it seems to be confusing me still.
Here is an example of what I’m trying to do:

Here is the link: https://example.com/redirect/abc?123

And I’d like it to redirect to the page: https://example.com/newlink/abc – Which removes the ?123 and redirecting to that

Edit:
The website specifically is a shortlink website and theres a website that does it but adds something next to the link like https://example.com/newlink/NewLink?SomethingHere which then breaks it – I’m trying to find a way to remove the ?SomethingHere part, if you get what I mean.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How would I do that in PHP specifically?

>Solution :

Using parse_url() which breaks up the parts of a URL into an assoc array, you can simply do

$url = 'https://example.com/redirect/abc?123';
$bits = parse_url($url);

$NewLocation = $bits['scheme'] . '://' . $bits['host'] . $bits['path'];
echo $NewLocation;

RESULT

https://example.com/redirect/abc

If you were to print_r($bits) you would see the array like this

Array
(
    [scheme] => https
    [host] => example.com
    [path] => /redirect/abc
    [query] => 123
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading