Viewed   64 times

I'm using file_get_contents() to grab content from a site, and amazingly it works even if the URL I pass as argument redirects to another URL.

The problem is I need to know the new URL, is there a way to do that?

 Answers

5

You might make a request with cURL instead of file_get_contents().

Something like this should work...

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if(preg_match('#Location: (.*)#', $a, $r))
 $l = trim($r[1]);

Source

Tuesday, November 29, 2022
3
<?php
    header("Status: 301 Moved Permanently");
    header("Location:./content/index.html?". $_SERVER['QUERY_STRING']);
    exit;
?>
Tuesday, September 27, 2022
 
2

It seems that your nginx configuration is causing the problems.

Its totally possible that nginx is modifying the response headers. This is not by default - you could have a configuration that is aimed for it to behave as a reverse proxy etc.

Have you tried testing the redirect on a nginx with its default configuration?

Thursday, September 29, 2022
4

You should be able to find it in Request.RawUrl.

Wednesday, October 19, 2022
2

My Problem is now solved, thanks to google and Daniweb here is solution

Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("Your short URL here"), HttpWebRequest)
        Dim response As HttpWebResponse
        Dim resUri As String
        response = req.GetResponse
        resUri = response.ResponseUri.AbsoluteUri
        MsgBox(resUri)

this will return URL_2.

Monday, September 12, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :