How can I get the destination URL using cURL?

How can I get the destination URL using cURL?

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

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Redirected URLs with cURL

Handling HTTP redirects requires a shift in how you configure your request. When curl_exec encounters a 302 Found or 301 Moved Permanently response, it automatically follows the redirect by default. This behavior is convenient for user agents but problematic when you specifically need to inspect the Location header returned on the initial request before the client navigates to the new destination.

In your PHP example, the variable $html will contain the body of the final page (the one the browser actually visited), and curl_getinfo($ch, CURLINFO_HTTP_CODE) will return the status code of that final step, which is almost certainly 200 OK. To capture the original redirect URL, you must tell cURL to stop following the redirection chain immediately.

Disabling Automatic Follows

The critical configuration option here is CURLOPT_FOLLOWLOCATION. By default, this is set to true. You need to explicitly set it to false to force cURL to return the response associated with the redirect status code rather than the final resource.

$url = "http://www.ecs.soton.ac.uk/news/";
$ch = curl_init();

// Disable automatic redirection following
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$html = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($status_code === 302 || $status_code === 301) {
    // Retrieve the Location header directly from the redirect response
    $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    
    // If you also need to inspect the header value specifically
    $location_header = curl_getinfo($ch, CURLINFO_HEADER_OUT) ? 
        (strpos(curl_getinfo($ch, CURLINFO_HEADER_OUT), 'Location:') !== false ? true : false) : false;
        
    echo "Original URL: " . curl_getinfo($ch, CURLINFO_URL) . "\n";
    echo "Status Code: $status_code\n";
    echo "Destination (Effective) URL: $redirect_url\n";
}

curl_close($ch);

Understanding CURLINFO_EFFECTIVE_URL

When you set CURLOPT_FOLLOWLOCATION to false and receive a 302, the $html variable is irrelevant because the server hasn't sent the final content yet. Instead, you should use curl_getinfo($ch, CURLINFO_EFFECTIVE_URL). This specific constant returns the URL of the last received response from the server. In the context of a redirect without following, this corresponds to the URL specified in the Location header.

This approach ensures that $redirect_url holds the exact destination address the client was intended to go to, preserving the logic flow for your application's decision-making process.

The Role of Header Inspection

While CURLINFO_EFFECTIVE_URL provides the cleanest string representation of the destination, sometimes you need to verify the raw header or handle multiple redirects in a single request object. You can access the raw header data using curl_getinfo($ch, CURLINFO_HEADER_OUT). This returns the entire HTTP headers as a string.

By parsing this string for the Location: key, you can confirm that cURL received a redirect instruction even if your specific use case doesn't require the parsed URL. However, relying on string parsing is generally less robust than using the dedicated CURLINFO_EFFECTIVE_URL constant, which abstracts away the header format differences between servers.

Handling 301 vs 302 Semantics

Your code checks for both 301 (Moved Permanently) and 302 (Found). While cURL treats them similarly regarding redirection mechanics, their semantic meaning differs significantly in a production environment. A 301 implies the resource has permanently moved, whereas a 302 suggests a temporary move.

When disabling CURLOPT_FOLLOWLOCATION, you are essentially acting as a strict HTTP client that refuses to navigate away from the origin server without explicit configuration. This is vital for security scanning or audit tools where you need to log exactly which URL triggered the redirect and what the target was, rather than silently visiting the final destination.

If you ever find yourself needing to follow redirects but still want to capture the first redirect URL, you would typically enable CURLOPT_FOLLOWLOCATION, execute the request, and then inspect the headers of the intermediate responses if your cURL version supports returning the full history (which is not standard in basic PHP bindings). Therefore, disabling the follow flag remains the most direct and reliable method for this specific requirement.

Integration with Modern Ecosystems

While raw cURL commands are effective for standalone scripts, frameworks like Laravel often abstract these low-level details. If you are working within a Laravel application, you might consider using the Symfony\Component\HttpClient component or the native Guzzle client, which offer more elegant ways to inspect redirect history without toggling verbose flags manually. However, for legacy scripts or CLI tools where dependency bloat is a concern, the raw cURL method described above remains the industry-standard solution.

Our network