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 redirecthttps://www.salferrarello.com/redirectme
should redirecthttps://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
============================
Leave a Reply