Last active
January 31, 2024 22:31
-
-
Save mttjohnson/be82e9faff00525b97294018a50d018b to your computer and use it in GitHub Desktop.
Testing HTTP Redirects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The check_url recursive function | |
check_url() { | |
THIS_URL="${1}" | |
HTTP_RESP_CODE=$(curl -ksI "${THIS_URL}" | grep -i 'HTTP/' | cut -d' ' -f 2) | |
echo "${THIS_URL} -> ${HTTP_RESP_CODE}" | |
if [ "${HTTP_RESP_CODE}" == "301" ] || [ "${HTTP_RESP_CODE}" == "302" ] | |
then | |
HTTP_LOC=$(curl -ksI ${1} | grep -i 'Location: ' | cut -d'_' -f 2) | |
HTTP_REDIRECT=$(echo "${HTTP_LOC}" | tail -c +11 | tr -d '\r' | tr -d '\n') | |
check_url "${HTTP_REDIRECT}" | |
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