Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 485
Weboide Avatar asked Sep 26 '10 18:09

Weboide


People also ask

How do I redirect a URL?

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.

Is HTTP redirect get or post?

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.

Which method takes a URL for redirection?

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.

What is redirect http code?

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.


1 Answers

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.

like image 72
webjay Avatar answered Oct 02 '22 23:10

webjay