• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Draft / Curl Get Redirect

Curl Get Redirect

Last updated on September 25, 2018 by Sal Ferrarello

When creating 301 redirects, I often want to check things quickly from the command line (to avoid the manual clicking in the browser and browser caching of results).

Traditionally, I’ve done this with curl. e.g.

curl -IL https://salcode.com

The -I flag (or you can use --head) tells curl, I only want to display the HTTP headers (not the whole reply).

The -L flag (or you can use --location) tells curl, follow any redirects to the final destination.

Example curl -IL

$ curl -IL https://salferrarello.com/redirectme

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 25 Sep 2018 14:59:58 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: https://salferrarello.com/curl-get-redirect
Cache-Control: max-age=172800
Expires: Thu, 27 Sep 2018 14:59:58 GMT

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 25 Sep 2018 14:59:59 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Cache-Enabled: True
Link: <https://salferrarello.com/wp-json/>; rel="https://api.w.org/"
Set-Cookie: wpSGCacheBypass=0; expires=Tue, 25-Sep-2018 13:59:59 GMT; Max-Age=0; path=/
Cache-Control: max-age=172800
Expires: Thu, 27 Sep 2018 14:59:59 GMT
Host-Header: 192fc2e7e50945beb8231a492d6a8024

This is great for checking a single redirect. Often, I want to check multiple urls (some should redirect and some should not):

  • https://salferrarello.com/redirectme should redirect
  • https://www.salferrarello.com/redirectme should redirect
  • https://salferrarello.com/redirectmee should NOT redirect

To make my life easier, I’ve added the following function to my .bashrc file.

# BEGIN curlGetRedirect
#
# Usage:
# curlGetRedirect <url>
#
# Output:
# URL: <url>
# HTTP/1.1 <HTTP status code, e.g. "301 Moved Permanently">
# Location: <url redirect points to>
#
# Example:
# $ curlGetRedirect http://salcode.com
# URL: http://salcode.com
# HTTP/1.1 301 Moved Permanently
# Location: https://salferrarello.com/
# ============================
function curlGetRedirect() {
    URL="$1"
    echo "URL: $URL"

    # --silent   Do not display progress meter or error messages.
    # --head     Display only HTTP headers (no content).
    # --location Follow redirects to final destination.
    curl --silent --head --location $URL | grep -e "^HTTP" -e "^Location"
    # grep filters the results leaving only those that match one of
    # the two -e conditions:
    # - "^HTTP" the line starts with HTTP
    # - "^Location" the line starts with Location

    echo "============================"
}
# END curlGetRedirect

Now, I can use curlGetRedirect to show only the information I want. e.g.

$ curlGetRedirect https://salferrarello.com/redirectme
URL: https://salferrarello.com/redirectme
HTTP/1.1 301 Moved Permanently
Location: https://salferrarello.com/curl-get-redirect
HTTP/1.1 200 OK
============================

this makes my life much easier when I want to check multiple URLs (particularly when I run this multiple times as I modify my redirect rules).

$ curlGetRedirect https://salferrarello.com/redirectme;curlGetRedirect https://www.salferrarello.com/redirectme;curlGetRedirect https://salferrarello.com/redirectmee
URL: https://salferrarello.com/redirectme
HTTP/1.1 301 Moved Permanently
Location: https://salferrarello.com/curl-get-redirect
HTTP/1.1 200 OK
============================
URL: https://www.salferrarello.com/redirectme
HTTP/1.1 301 Moved Permanently
Location: https://salferrarello.com/curl-get-redirect
HTTP/1.1 200 OK
============================
URL: https://salferrarello.com/redirectmee
HTTP/1.1 200 OK
============================
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Dev Tips, Draft, Solution Tagged With: command line, curl, redirects

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in