php get url of redirect from source url

php get url of redirect from source url

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Resolve Redirects in PHP Without Following Them

When dealing with feed aggregators like libero-news.it.feedsportal.com, you often encounter a scenario where the initial URL is merely an intermediate proxy. A request to that endpoint returns HTTP 302 or 301, instructing your client to redirect to the canonical destination. The challenge arises when you need to extract that final destination without actually performing the redirect yourself.

The code snippet provided in the source material attempts to solve this by using curl_exec with CURLOPT_FOLLOWLOCATION set to false and inspecting the response body for a Location: header. However, this approach is fundamentally flawed because it misunderstands how HTTP redirects are communicated. When CURLOPT_FOLLOWLOCATION is disabled, cURL does not follow the redirect; instead, it returns the original request's response status code (usually 302) and stores the Location header in the response headers, not in the body of the HTML content.

To capture the final URL correctly, you must inspect the raw HTTP headers returned by cURL. The specific function for this is curl_getinfo(). By retrieving the redirect_url info key, you can access the destination exactly as cURL would have followed it had you enabled the flag, but without actually navigating to the new page and fetching its content.

Here is the corrected implementation:

$url = "http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Disable following redirects so we stay on the initial request context
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
// Return headers and body in a single string or just headers
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);

curl_close($ch);

if ($httpCode === 302 && $redirectUrl) {
    echo "Detected Redirect: " . $redirectUrl;
} else {
    // Fallback: Parse the Location header manually if redirect_url is empty
    // This handles cases where the server sends headers but curl_getinfo doesn't populate it immediately
    if (preg_match('#Location:\s*(.*)#i', $response, $matches)) {
        echo "Detected Redirect: " . trim($matches[1]);
    } else {
        echo "No redirect detected or already at final URL.";
    }
}

Why curl_getinfo is Preferable

Using curl_getinfo($ch, CURLINFO_REDIRECT_URL) is the robust standard for this task. It abstracts away the complexity of parsing raw header strings. The function returns the destination URL if a redirect occurred and CURLOPT_FOLLOWLOCATION was false. If no redirect happened, it returns an empty string.

However, relying solely on the info array can sometimes be brittle depending on the cURL version or specific server configurations. A safer pattern involves checking the HTTP status code first. If the status code is 301 or 302 and CURLINFO_REDIRECT_URL is empty, you should parse the raw headers using a regex against the response string. This hybrid approach ensures compatibility across different environments.

Performance and Security Considerations

Inspecting redirect chains without following them is efficient because it prevents your script from downloading the full payload of the target page if you only need the URL. However, there is a significant risk if you blindly trust the Location header. Attackers can exploit open redirects by pointing a legitimate domain to a malicious site. Always validate that the resolved URL belongs to a trusted domain before appending it to your application or sending it to a user.

In PHP frameworks like Laravel, you might encounter similar scenarios when fetching RSS feeds or processing webhooks. While php://input wrappers or third-party libraries exist, raw cURL remains the most transparent tool for inspecting low-level HTTP behavior. If you are building a larger application where feed aggregation is part of a content pipeline, consider leveraging existing packages that handle feed parsing and header extraction to reduce maintenance overhead.

Our network