How to Get Final Url After Following Http Redirections in Pure PHP

How to get final URL after following HTTP redirections in pure PHP?

/**
* get_redirect_url()
* Gets the address that the provided URL redirects to,
* or FALSE if there's no redirect.
*
* @param string $url
* @return string
*/
function get_redirect_url($url){
$redirect_url = null;

$url_parts = @parse_url($url);
if (!$url_parts) return false;
if (!isset($url_parts['host'])) return false; //can't process relative URLs
if (!isset($url_parts['path'])) $url_parts['path'] = '/';

$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
if (!$sock) return false;

$request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
$request .= 'Host: ' . $url_parts['host'] . "\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($sock, $request);
$response = '';
while(!feof($sock)) $response .= fread($sock, 8192);
fclose($sock);

if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
if ( substr($matches[1], 0, 1) == "/" )
return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
else
return trim($matches[1]);

} else {
return false;
}

}

/**
* get_all_redirects()
* Follows and collects all redirects, in order, for the given URL.
*
* @param string $url
* @return array
*/
function get_all_redirects($url){
$redirects = array();
while ($newurl = get_redirect_url($url)){
if (in_array($newurl, $redirects)){
break;
}
$redirects[] = $newurl;
$url = $newurl;
}
return $redirects;
}

/**
* get_final_url()
* Gets the address that the URL ultimately leads to.
* Returns $url itself if it isn't a redirect.
*
* @param string $url
* @return string
*/
function get_final_url($url){
$redirects = get_all_redirects($url);
if (count($redirects)>0){
return array_pop($redirects);
} else {
return $url;
}
}

And, as always, give credit:

http://w-shadow.com/blog/2008/07/05/how-to-get-redirect-url-in-php/

Get Final Redirect URL on Tricky Redirects

I Found it my function don't follow javascript and meta tag redirection you can use regex to read the html code and find the javascript/jQuery or meta tag redirection

code to get url form javascript and meta tag redirection :

if (preg_match('/window\.location\.replace\([\s]{0,}[\"\'](.*)[\"\'][\s]{0,}\)/i', $response, $redirect_result['1']) ||
preg_match('/\$\(location\)\.attr\([\s]{0,}[\"\'][\s]{0,}href[\s]{0,}[\"\'][\s]{0,}\,[\s]{0,}[\"\'][\s]{0,}(.*)[\s]{0,}[\"\'][\s]{0,}\)/i', $response, $redirect_result['2']) ||
preg_match('/window\.location[\s]{0,}\=[\s]{0,}[\"\'](.*)[\"\']/i', $response, $redirect_result['3']) ||
preg_match('/window\.location\.href[\s]{0,}\=[\s]{0,}[\"\'](.*)[\"\']/i', $response, $redirect_result['4']) ||
preg_match('/window\.href[\s]{0,}\=[\s]{0,}[\"\'](.*)[\"\']/i', $response, $redirect_result['5']) ||
preg_match('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $response, $redirect_result['6'])) {
for ($i = 1; $i <= 6; $i++) {
if ($redirect_result[$i]) {
$window_location_final = $redirect_result[$i];
break;
}
}
$window_location_final = end($window_location_final);
if (substr($window_location_final, 0, 1) === '/' && valid_url(trim($redirect_url))) {
$window_location_final = rtrim($redirect_url, '/') . $window_location_final;
}
$window_location_final = valid_url(trim($window_location_final), TRUE) ? trim($window_location_final) : '';
if ($window_location_final) {
$redirect_url = $window_location_final;
}
return $redirect_url;
}

Is there a way I can get the redirect URL without loading the content?

using curl:

$url = 'https://partner.api.beatsmusic.com/v1/api/tracks/tr58141709/images/default?client_id=XXXXXXX';
$ch = curl_init($url);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

// check if we've received a redirect in the Location header
if (array_key_exists('redirect_url', $info)) {
// do something with the Location header value
echo $info['redirect_url'];
}

Redirect 1 domain to other base on url ending

This should work. Put this in the index.php file for redirect.com:

<?php
header('Location: http://base.com/q='.$_GET["q"]);
?>


Related Topics



Leave a reply



Submit