Simplifying URL Redirection Checks with a Bash Script

Simplifying URL Redirection Checks with a Bash Script
Photo by Vishnu MAS / Unsplash

When working with websites, it's common to encounter URL redirections. A redirection happens when a web address points to another address. For instance, you may visit http://buka.sh, but your browser is sent to https://buka.sh. Understanding how to check these redirections can be very helpful, especially for web developers and SEO specialists. In this article, we'll create a simple Bash script that will help you easily check the final destination of any URL after following its redirections.

Why Check for Redirections?

There are several reasons you might want to check for URL redirections. For one, ensuring that your website properly redirects traffic can improve user experience. If users land on an outdated URL, they should seamlessly be taken to the correct page without confusion. Additionally, search engines prefer sites that correctly handle redirects, which can positively impact your website's SEO.

Creating the Script

Let’s dive into the script. Open your favorite text editor and create a new file named check_url_redirect.sh. You can use any editor like nano, vim, or gedit.

Here’s the complete script:

#!/bin/bash
for url in "$@"; do
  final_url=$(curl -s -o /dev/null -w "%{url_effective}" -L "$url")
  echo "$url => $final_url"
done

Understanding the Script

Let’s break down what this script does. The first line, #!/bin/bash, tells the system to use the Bash shell to execute the script. The for url in "$@"; do line means that the script will iterate through each URL you provide as an argument when running it.

Inside the loop, we use curl, a powerful command-line tool for transferring data with URLs. The options we use are important:

  • -s: This option enables silent mode, which suppresses progress and error messages, allowing for cleaner output.
  • -o /dev/null: This option tells curl to discard the output. We’re not interested in the webpage content, just the final URL.
  • -w "%{url_effective}": This tells curl to print the final effective URL after following any redirects.
  • -L: This option instructs curl to follow redirects until it reaches the final destination.

Finally, we echo the original URL and the final URL to the terminal, formatted as original URL => final URL.

Running the Script

To execute the script, ensure it has the proper permissions. You can make it executable with the following command:

chmod +x check_url_redirect.sh

Now, you can run the script by passing the URLs as arguments:

./check_url_redirect.sh http://buka.sh https://buka.sh https://www.buka.sh

This will output something like:

http://buka.sh => https://buka.sh/
https://buka.sh => https://buka.sh/
https://www.buka.sh => https://buka.sh/
https://buka.sh => https://buka.sh/

Additional Points to Consider

While this script is a great starting point, there are a few enhancements you could consider.

  • Error Handling: You might want to add checks for invalid URLs or handle timeouts. This can help avoid confusion if a URL fails to respond.
  • Output Formatting: Consider using colors or additional formatting to make the output more readable and visually appealing.
  • Saving Results: You could extend the script to save results to a file for later analysis, which can be helpful for larger batches of URLs.
  • Checking HTTPS: You might want to specifically check if the final URL uses HTTPS for security purposes.
GitHub - sonyarianto/check-url-redirect: Bash script to check url redirects
Bash script to check url redirects. Contribute to sonyarianto/check-url-redirect development by creating an account on GitHub.

Finally

This simple Bash script is an effective way to check URL redirections and can be particularly useful for developers and SEO professionals. By understanding how to use curl and Bash scripting, you can streamline the process of verifying URLs and enhance your web development skills. Remember, ensuring that your URLs correctly redirect can greatly improve user experience and search engine rankings.

If you want online tool that doing similar things, you can read this post about httpstatus.io.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe