2

I need to get the URL of a redirected page in PHP.

Let's say I have an URL like http://example.com, while I open this, it redirects to http://redirected_url.com. So the input and output of this PHP function must be like this:

  • Input: http://example.com
  • Output: http://redirected_url.com

How can I do this?

Edit: I don't mean redirection. I need the url of redirected page. So I don't know redirected url, but I need to find it.

hakre
  • 193,403
  • 52
  • 435
  • 836
JohnUS
  • 1,001
  • 4
  • 11
  • 13
  • You should clarify your question in the first place ! Also what have you tried so far ? – Sarfraz Feb 24 '12 at 15:52
  • You need to show what you have tried. You need to clarify which location you are able to edit. – rockerest Feb 24 '12 at 15:54
  • The PHP function which I need mustn't redirect page. It must just echo the redirected url. like this : echo $output;. So I need to find string. – JohnUS Feb 24 '12 at 15:56
  • Possible duplicate of: [HTTP response code after redirect](http://stackoverflow.com/questions/7566225/http-response-code-after-redirect) – hakre Jul 08 '12 at 08:38
  • Why was this closed? This is a perfectly legitimate question. This is also exactly what I'm looking for. Also, it is not a "possible duplicate" referred by the comment above. – earth2jason Apr 15 '15 at 03:05

2 Answers2

11

It's hard to tell from the question, but if you're wanting to tell if a url you're accessing is trying to redirect you, you can use get_headers("http://some_url") and check in key[0] of the response if it's sent you a 301 status code. Or if it has a "location" header like is mentioned above. php docs

Like this:

function get_redirect_target($destination){
    $headers = get_headers($destination, 1);
    return $headers['Location'];
}

I'll leave error (and case sensitivity) handling up to you, but that should get you there

menacingly
  • 728
  • 4
  • 11
  • I don't need it. All the pages which I have are redirected pages. I need to find redirected url. – JohnUS Feb 24 '12 at 16:02
  • There are only two URLs we're talking about. The one you accessed, and the one it's sending you to. You have to already have the first one to make the query, and this method gives you the second. The "Location" header will contain the url it's attempting to send you to. If this still isn't what you want, I would advise a complete rewrite of the question since no one is understanding it. – menacingly Feb 24 '12 at 16:07
  • There can be chains of redirects, too. Also there can be "relative" URLs with redirects. I linked a question [HTTP response code after redirect](http://stackoverflow.com/questions/7566225/http-response-code-after-redirect) which contains more detailed information. – hakre Jul 08 '12 at 08:39
  • Undefined index: Location – earth2jason Apr 15 '15 at 03:05
-1
header('Location: http://redirected_url.com');
MetalFrog
  • 9,943
  • 1
  • 22
  • 24
  • It's funny because neither one of you guys got the format correct, according to web standards: [It should be "Location"](http://tools.ietf.org/html/rfc2616#section-14.30) (no downvote) – rockerest Feb 24 '12 at 15:52
  • As unclear/unresearched as the question is, he's probably right in downvoting this, as it is at least clear that that's not what he was asking. – Dave Feb 24 '12 at 15:53
  • I don't mean redirection. I need the url of redirected page. So I don't know redirected url, but I need to find it. – JohnUS Feb 24 '12 at 15:53
  • Good catch, rockerest. John, how do you know that the url has redirected? – MetalFrog Feb 24 '12 at 15:55
  • All the pages which I have are redirected pages. So it is not a problem – JohnUS Feb 24 '12 at 15:58