Skip to content

Instantly share code, notes, and snippets.

@mttjohnson-jf
Forked from mttjohnson/test_http_redirects.sh
Last active January 31, 2024 23:21
Show Gist options
  • Select an option

  • Save mttjohnson-jf/8e1f393f7999f10d1ad175f7a50925ff to your computer and use it in GitHub Desktop.

Select an option

Save mttjohnson-jf/8e1f393f7999f10d1ad175f7a50925ff to your computer and use it in GitHub Desktop.
Testing HTTP Redirects
# The check_url recursive function
check_url() {
this_url="${1}"
curl_format=$(echo "
http_code| %{http_code}
http_version| %{http_version}
\n")
# (
# cat << EOF
# curl -w "${curl_format}" -o /dev/null -D - -sk "${this_url}"
# EOF
# )
http_resp=$(curl -w "${curl_format}" -o /dev/null -D - -sk "${this_url}")
http_resp_code=$(echo "${http_resp}" | grep -i 'http_code' | cut -d '|' -f 2 | tr -d '[:space:]')
echo "${this_url} -> ${http_resp_code}"
# echo "${http_resp}"
if [[ "${http_resp_code}" == "301" ]] || [[ "${http_resp_code}" == "302" ]]; then
http_loc=$(echo "${http_resp}" | grep -i 'location' | cut -d ' ' -f 2 | tr -d '[:space:]')
# echo " ---- ${http_loc}"
check_url "${http_loc}"
fi
}
# Test a single URL
check_url "http://example.com"
# Test a list of URLs
URL_LIST="
http://example.com
http://google.com
http://www.google.com
"
echo "${URL_LIST}" | while read EACH_URL
do
if [[ "${EACH_URL}" == "" ]]; then continue; fi
echo "-----------------${EACH_URL}-----------------"
check_url "${EACH_URL}"
echo "----------------------------------"
done
# Construct a list of URLs to test from the first column of a .csv file
BASE_URL="https://example.com"
FILENAME="example-redirects.csv"
URL_LIST=$(cat ${FILENAME} | perl -pe 's#^(.*),.*$#'${BASE_URL}'$1#g') # Extract first column from .csv file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment