Categories
SysOps

How to display real destination URL

I am using curl to verify redirects and display real destination URL, and you should try it too.

Command-line

The simplest way to display a real destination URL is to use quiet mode, discard output, follow redirects and display the URL that was fetched in the last transfer after executing GET method.

$ curl --silent --location --output /dev/null --write-out "%{url_effective}\n" http://sleeplessbeastie.eu
https://sleeplessbeastie.eu/

This solution can be extended to fetch only headers instead of the whole document.

$ curl --head --silent --location --output /dev/null --write-out "%{url_effective}\n" http://sleeplessbeastie.eu
https://sleeplessbeastie.eu/

You can be more verbose and display the number of performed redirects.

$ curl --head --silent --location --output /dev/null --write-out "It took %{num_redirects} redirect/redirects to get %{url_effective}\n" http://sleeplessbeastie.eu
It took 1 redirect/redirects to get https://sleeplessbeastie.eu/

Shell script

Use the following shell script to iterate over every redirection and display it in a human-readable form.

#!/bin/bash
# Display redirects performed for given URL

if [ "$#" != "1" ]; then
  echo "Display redirects performed for given URL"
  echo "URL is missing"
  exit
fi

# URL from/to
curl_url_from=""
curl_url_to="$1"

# number of performed redirects
curl_num_redirects="0"

# use head instead of get?
curl_use_head=0

# define head or get method
if [ "$curl_use_head" -eq "1" ]; then
  curl_param_head="--head"
else
  curl_param_head=""
fi

# main loop
while true; do
  # execute curl to get from/to URL
  curl_output="$(curl $curl_param_head --silent --output /dev/null --write-out " %{url_effective} %{redirect_url}\n" $curl_url_to)"
  # calculate number of received parameters
  curl_output_n="$(echo $curl_output | awk '{print NF}')"
  if [ "$curl_output_n" -eq "2" ]; then
    # number of received parameters is equal to 2
    # it means that there was redirection

    # set from/to URL
    curl_url_from="$(echo $curl_output | awk '{print $1}')"
    curl_url_to="$(echo $curl_output | awk '{print $2}')"

    # get http code
    curl_http_code="$(curl $curl_param_head --silent --output /dev/null --write-out "%{http_code}\n" $curl_url_to)"
    echo "${curl_url_from} -> ${curl_url_to} (${curl_http_code})"

    # increase number of redirects
    curl_num_redirects=$(expr $curl_num_redirects \+ 1)
  else
    # number of received parameters is equal to 1
    # it means that there was no redirection

    if [ "$curl_num_redirects" -gt "0" ]; then
      echo
    fi
    echo "Number of performed redirects: $curl_num_redirects"
    break
  fi
done

Shell script – examples

Standard HTTP to HTTPS redirect:

$ bash display_redirects.sh http://sleeplessbeastie.eu
http://sleeplessbeastie.eu/ -> https://sleeplessbeastie.eu/ (200)

Number of performed redirects: 1

No redirect:

$ bash display_redirects.sh https://sleeplessbeastie.eu
Number of performed redirects: 0

Standard HTTP to HTTPS redirect with 404 HTTP code:

$ bash display_redirects.sh http://sleeplessbeastie.eu/non-existent
http://sleeplessbeastie.eu/non-existent -> https://sleeplessbeastie.eu/non-existent (404)

Number of performed redirects: 1

Multiple redirects:

$ bash display_redirects.sh http://clickmeeting.com
http://clickmeeting.com/ -> http://clickmeeting.com/pl (301)
http://clickmeeting.com/pl -> https://clickmeeting.com/pl (200)

Number of performed redirects: 2