View Full Version : Here is a tricky one: Whats the quickest way to get the final url from a redirection?

02-15-2010, 10:54 AM

I want to capture the final url that a website redirects to.

Here is an example of what I mean:


When you click on that link, the site will redirect you to


As you can see they are two different sites.

What I would like to do is pick the
www.Joe-Blogs.com/green/prod1.html part of the final url
and put it in a variable called $final_url.

So if I have :

$first_url = "www.example.com/sites.php?pd=45";

What would be the best way to get to that $final_url.

Should I be using cUrl or would
file() or get_file_contents() be able to get the url ?

Any ideas on how I can get to my $final_url ?


02-15-2010, 04:41 PM
I just thought that it may look like I am trying
to build a proxy, but I am not.

(there are plenty of proxies around already ! )

I want to use the resulting url in another third party website that does free
analysis based on the url.

So if you have any ideas on how I would get a script to follow the
re-direct an just get the final url... I am all ears :)


02-15-2010, 09:15 PM
If I use cUrl, the code below should get the to the final webpage right?

Is the final destination in the HEADER info ?

$target_url = "www.example.com/sites.php?pd=45";
$cef = "curl_err.txt";
$ceh = fopen($cef, 'w');

curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_STDERR, $ceh);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$output = curl_exec($ch);
$info = curl_getinfo($ch);

How would I extract the final url ?

Any ideas ?


02-18-2010, 04:16 PM
Doesn't anyone know how to do this ?

Maybe I should use be using
fsockopen() to get the final url?

If any one can help me with this one I would really
appreciate as I am pretty stuck :(