Reputation: 3
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.
How would I do that in PHP specifically?
Upvotes: 0
Views: 620
Reputation: 31
You can use strtok
function to remove the parameters after the URL, and use the following instruction header("Location: ...")
to perform the redirection.
Take a look at the following example:
$url = strtok($url, '?');
header("Location: $url")
Upvotes: 1
Reputation: 94682
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
)
Upvotes: 4