Skip to content

Instantly share code, notes, and snippets.

@grayayer
Last active April 15, 2025 17:50
Show Gist options
  • Save grayayer/d39173325159206e080f353a6aa5a5fb to your computer and use it in GitHub Desktop.
Save grayayer/d39173325159206e080f353a6aa5a5fb to your computer and use it in GitHub Desktop.
Takes your domain list file as input Tests each domain to see if it properly redirects to your target
Creates a timestamped report file (e.g., redirect_verification_report_20250415_120530.txt)
Records all failures, timeouts, and errors in the report file
Includes detailed information about each issue
Adds a summary section at the end of the report
Automatically deletes the report file if all redirects work correctly (no failures or errors)
Provides a clear message about where to find the report if issues are detected
When you run this script:
If everything works perfectly, you'll see a success message and no report file will be created
If there are any issues, you'll see a message indicating where to find the detailed report
The report file format is clean and easy to read:
REDIRECT VERIFICATION REPORT
Generated on: Tue Apr 15 12:05:30 PDT 2025
Expected redirect target: https://compuweather.com/
================================================
[FAIL] https://example.org - Redirects to https://example.org/ (expected https://compuweather.com/)
[TIMEOUT] https://slow-domain.net - Connection timed out after 10 seconds
[ERROR] https://missing-domain.com - curl exit code: 6
SUMMARY
================================================
Successful redirects: 45
Failed redirects: 1
Errors/timeouts: 2
Total domains checked: 48
This report gives you a clear record of any issues that need to be addressed, which you can then follow up on individually.
To use the script:
## INSTRUCTIONS
Save it as verify-redirects.sh
Make it executable: chmod +x verify-redirects.sh
Run it: ./verify-redirects.sh domain_list.txt
#!/bin/bash
# Configuration
EXPECTED_REDIRECT="https://compuweather.com/" # The expected redirect target (include trailing slash)
TIMEOUT=10 # curl timeout in seconds
REPORT_FILE="redirect_verification_report_$(date +%Y%m%d_%H%M%S).txt" # Report file name with timestamp
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to check a single domain redirect
check_redirect() {
local domain=$1
local protocol=${2:-"https"}
echo -n "Checking ${protocol}://${domain}... "
# Use curl to follow redirects and get the final URL
result=$(curl -s -L -o /dev/null -w "%{url_effective}" \
--connect-timeout $TIMEOUT \
--max-time $TIMEOUT \
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" \
"${protocol}://${domain}" 2>/dev/null)
exit_code=$?
# Check for curl errors
if [ $exit_code -ne 0 ]; then
if [ $exit_code -eq 28 ]; then
echo -e "${YELLOW}TIMEOUT${NC}"
echo "[TIMEOUT] ${protocol}://${domain} - Connection timed out after ${TIMEOUT} seconds" >> "$REPORT_FILE"
return 28
else
echo -e "${RED}ERROR (curl exit code: $exit_code)${NC}"
echo "[ERROR] ${protocol}://${domain} - curl exit code: $exit_code" >> "$REPORT_FILE"
return $exit_code
fi
fi
# Normalize URLs for comparison (remove trailing slash if present)
normalized_result=${result%/}
normalized_expected=${EXPECTED_REDIRECT%/}
# Compare the final URL with expected redirect
if [[ "$normalized_result" == "$normalized_expected" || "$result" == "$EXPECTED_REDIRECT" ]]; then
echo -e "${GREEN}SUCCESS${NC} (redirects to $result)"
return 0
else
echo -e "${RED}FAIL${NC} (redirects to $result, expected $EXPECTED_REDIRECT)"
echo "[FAIL] ${protocol}://${domain} - Redirects to $result (expected $EXPECTED_REDIRECT)" >> "$REPORT_FILE"
return 1
fi
}
# Function to process the list of domains
process_domain_list() {
local file_path=$1
local success_count=0
local fail_count=0
local error_count=0
local total_count=0
echo "=== Testing redirects for domains in $file_path ==="
echo "Expected redirect target: $EXPECTED_REDIRECT"
echo "----------------------------------------------"
# Initialize the report file with header
echo "REDIRECT VERIFICATION REPORT" > "$REPORT_FILE"
echo "Generated on: $(date)" >> "$REPORT_FILE"
echo "Expected redirect target: $EXPECTED_REDIRECT" >> "$REPORT_FILE"
echo "================================================" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
while IFS=, read -r domain zone_id || [[ -n "$domain" ]]; do
# Skip empty lines and lines starting with #
if [[ -z "$domain" || "$domain" =~ ^# ]]; then
continue
fi
# Remove any whitespace
domain=$(echo "$domain" | tr -d '[:space:]')
# Skip if domain is empty after trimming
if [[ -z "$domain" ]]; then
continue
fi
((total_count++))
# Try HTTPS first
check_redirect "$domain" "https"
result=$?
# If HTTPS fails with a connection error, try HTTP
if [ $result -eq 7 ]; then
echo "[INFO] Falling back to HTTP for $domain" >> "$REPORT_FILE"
check_redirect "$domain" "http"
result=$?
fi
# Count results
if [ $result -eq 0 ]; then
((success_count++))
elif [ $result -eq 28 ]; then
((error_count++))
else
((fail_count++))
fi
# Add a small delay between checks
sleep 0.5
done < "$file_path"
echo "----------------------------------------------"
echo "Summary:"
echo -e "${GREEN}Successful redirects: $success_count${NC}"
echo -e "${RED}Failed redirects: $fail_count${NC}"
echo -e "${YELLOW}Errors/timeouts: $error_count${NC}"
echo "Total domains checked: $total_count"
# Add summary to report file
echo "" >> "$REPORT_FILE"
echo "SUMMARY" >> "$REPORT_FILE"
echo "================================================" >> "$REPORT_FILE"
echo "Successful redirects: $success_count" >> "$REPORT_FILE"
echo "Failed redirects: $fail_count" >> "$REPORT_FILE"
echo "Errors/timeouts: $error_count" >> "$REPORT_FILE"
echo "Total domains checked: $total_count" >> "$REPORT_FILE"
# Return success if there were no issues
if [[ $fail_count -eq 0 && $error_count -eq 0 ]]; then
echo -e "\nAll redirects are working correctly!"
# If everything is successful, delete the report file
rm "$REPORT_FILE"
return 0
else
echo -e "\nFound issues with some redirects. See $REPORT_FILE for details."
return 1
fi
}
# Main execution
if [ "$1" ]; then
file_path="$1"
else
file_path="domain_list.txt"
fi
if [ ! -f "$file_path" ]; then
echo "Error: File $file_path not found"
echo "Usage: $0 [domain_list_file]"
echo "File format should be: domain.com,zone_id (one per line)"
exit 1
fi
# Check if curl is installed
if ! command -v curl &> /dev/null; then
echo "Error: curl is not installed. Please install curl to use this script."
exit 1
fi
# Run the tests
process_domain_list "$file_path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment