What I'd like to do is find out what is the last/final URL after following the redirections.
I would prefer not to use cURL. I would like to stick with pure PHP (stream wrappers).
Right now I have a URL (let's say http://domain.test), and I use get_headers() to get specific headers from that page. get_headers will also return multiple Location:
headers (see Edit below). Is there a way to use those headers to build the final URL? or is there a PHP function that would automatically do this?
Edit: get_headers() follows redirections and returns all the headers for each response/redirections, so I have all the Location:
headers.
Click the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.
In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3 , and a Location header holding the URL to redirect to. When browsers receive a redirect, they immediately load the new URL provided in the Location header.
A 301 redirect is a server-side redirect which redirects users from URL A to URL B, while signaling to search engines that URL A's content has been permanently moved to URL B. When it comes to redirects, the 301 redirect usually is your best choice.
The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.
function getRedirectUrl ($url) {
stream_context_set_default(array(
'http' => array(
'method' => 'HEAD'
)
));
$headers = get_headers($url, 1);
if ($headers !== false && isset($headers['Location'])) {
return $headers['Location'];
}
return false;
}
Additionally...
As was mentioned in a comment, the final item in $headers['Location']
will be your final URL after all redirects. It's important to note, though, that it won't always be an array. Sometimes it's just a run-of-the-mill, non-array variable. In this case, trying to access the last array element will most likely return a single character. Not ideal.
If you are only interested in the final URL, after all the redirects, I would suggest changing
return $headers['Location'];
to
return is_array($headers['Location']) ? array_pop($headers['Location']) : $headers['Location'];
... which is just if short-hand for
if(is_array($headers['Location'])){
return array_pop($headers['Location']);
}else{
return $headers['Location'];
}
This fix will take care of either case (array, non-array), and remove the need to weed-out the final URL after calling the function.
In the case where there are no redirects, the function will return false
. Similarly, the function will also return false
for invalid URLs (invalid for any reason). Therefor, it is important to check the URL for validity before running this function, or else incorporate the redirect check somewhere into your validation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With