Open In App

How to find where the URL will redirected using cURL?

In General, cURL stands for ‘Client for URLs’, here URL written in uppercase to indicates that the cURL deals with URLs.

PHP Approach:
Basic Function used in cURL:



Parameter with the other important commands:

Program:




<?php
  
// From URL to get redirected URL
  
// Initialize a CURL session.
$ch = curl_init();
  
// Grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_URL, $url);
  
// Catch output (do NOT print!)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  
// Return follow location true
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$html = curl_exec($ch);
  
// Getinfo or redirected URL from effective URL
$redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  
// Close handle
curl_close($ch);
echo "Original URL:   " . $url . "<br/>";
echo "Redirected URL: " . $redirectedUrl . "<br/>";
  
?> 

Output:

Original URL: https://geeksforgeeks.org/
Redirected URL: https://www.geeksforgeeks.org/

Command-Line Approach:

  1. Get Redirect URL with cURL:
    Syntax:
    curl -Ls -w %{url_effective} -o /dev/null [URL]
    

    Description:

    • curl — command name
    • -s — Silent mode
    • -L — Location which Follow redirects
    • -D – — Dump headers here
    • [URL] — URL that performs redirection
    • -o /dev/null — remove extra stdout info
    • -w ‘%{url_effective}’ — final destination

    Example 1:

    curl -Ls -w %{url_effective} -o /dev/null 
    https://www.geeksforgeeks.org/php-cucrl/
    

    Output:

  2. Follow the redirects with cURL:

    syntax:

    curl -s -L -D - [URL] -o /dev/null -w '%{url_effective}'
    

    Example 2: Following redirect to 404 Error page.

    curl -s -L -D - 
    https://www.geeksforgeeks.org/php-cucrl/ -o /dev/null -w 
    '%{url_effective}'
    

    Output:

    D:\mycurl\bin>curl -s -L -D - https://www.geeksforgeeks.org/php-cucrl/ 
    -o /dev/null -w '%{url_effective}'
    HTTP/2 404
    server: Apache
    strict-transport-security: max-age=3600; includeSubDomains
    link: ; rel="https://api.w.org/"
    access-control-allow-credentials: true
    x-frame-options: DENY
    x-content-type-options: nosniff
    content-type: text/html; charset=UTF-8
    cache-control: must-revalidate, max-age=3, s-maxage=21600
    date: Mon, 08 Jul 2019 01:34:28 GMT
    
    'https://www.geeksforgeeks.org/php-cucrl/'
    
  3. Followed redirect to correct page:
    Example 3:
    curl -s -L -D - 
    https://www.geeksforgeeks.org/php-curl/amp/ -o /dev/null -w 
    '%{url_effective}'
    

    Output:

    D:\mycurl\bin>curl -s -L -D - https://www.geeksforgeeks.org/php-curl/amp/ 
    -o /dev/null -w '%{url_effective}'
    HTTP/2 200
    server: Apache
    strict-transport-security: max-age=3600; includeSubDomains
    access-control-allow-credentials: true
    x-frame-options: DENY
    x-content-type-options: nosniff
    content-type: text/html; charset=UTF-8
    cache-control: must-revalidate, max-age=3, s-maxage=21600
    date: Mon, 08 Jul 2019 01:34:55 GMT
    
    'https://www.geeksforgeeks.org/php-curl/amp/'
    

Article Tags :