Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get HTTP redirect destination for a URL in PHP
<?php
// FOLLOW A SINGLE REDIRECT:
// This makes a single request and reads the "Location" header to determine the
// destination. It doesn't check if that location is valid or not.
function get_redirect_target($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = curl_exec($ch);
curl_close($ch);
// Check if there's a Location: header (redirect)
if (preg_match('/^Location: (.+)$/im', $headers, $matches))
return trim($matches[1]);
// If not, there was no redirect so return the original URL
// (Alternatively change this to return false)
return $url;
}
// FOLLOW ALL REDIRECTS:
// This makes multiple requests, following each redirect until it reaches the
// final destination.
function get_redirect_final_target($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // set referer on redirect
curl_exec($ch);
$target = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
if ($target)
return $target;
return false;
}
@herlon214

This comment has been minimized.

Copy link

commented Sep 5, 2014

Thanks!

@gieart87

This comment has been minimized.

Copy link

commented Nov 13, 2014

what if the url on the go using an iframe , and the final destination of the redirect url in the iframe content ? I tried it with your function but do not go well. Could you give me suggestion?. thanks,

@setyawanandik

This comment has been minimized.

Copy link

commented Jan 3, 2016

Can this set ?? emmm request from client detect ??
so , request not from server host but request from client.

@khoand0000

This comment has been minimized.

Copy link

commented Apr 23, 2016

Thanks

@alvnfaiz

This comment has been minimized.

Copy link

commented Feb 10, 2018

helped

@KentuckySato

This comment has been minimized.

Copy link

commented Feb 16, 2018

Thanks

@poojarfuturistic5

This comment has been minimized.

Copy link

commented Jun 28, 2018

I am loading entered url content in iframe but need proper entered url redirects
by using above code it redirects to entered domain
I need to stay on same page and load in iframe

@01678424113

This comment has been minimized.

Copy link

commented Nov 27, 2018

thanks you so much

@g-giani

This comment has been minimized.

Copy link

commented Nov 28, 2018

Thx!

@ihrifat2

This comment has been minimized.

Copy link

commented Mar 13, 2019

Both functions return the first URL (response code 301), not the final URL

Screen_Shot_2019-03-13_at_12 22 54

@shadowc0de

This comment has been minimized.

Copy link

commented Apr 2, 2019

thanks dude

@oneNevan

This comment has been minimized.

Copy link

commented May 2, 2019

Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.