I do not understand why you need PHP or JavaScript. I mean ... they are slightly different from the approach to the problem.
Assuming you want to use a server side PHP solution, there is a comprehensive solution here . Too much code to copy verbatim, but:
function follow_redirect($url){
$redirect_url = null;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$pos = strpos($response, "Location: ");
if($pos === false){
return false;
} else {
$pos += strlen($header);
$redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos);
return $redirect_url;
}
}
while(($newurl = follow_redirect($url)) !== false){
echo $url, '<br/>';
$url = $newurl;
}
source
share