Redirect target and destination

Somewhere someone’s complaining about a redirecting URL that’s not going where it’s supposed to. Or maybe the URL is hopping too many times and it’s affecting SEO. Using cURL on the command line is a quick and clear way to see where a URL is redirecting to and how it’s getting there.

In the example below, the target is the URL to be redirected and the destination is where we want the user to end up.

Target Destination
http://test.chrislatta.org/myredirect.html http://test.chrislatta.org/lost.html

When viewing this in the web browser we don’t see a problem, the target redirects to the destination. But we request the target URL with cURL, we’ll see there are several extra redirects between the two.

Follow the redirects with cURL

We’ll use the following cURL command and options to get each step of our redirects.

curl -s -L -D - http://test.chrislatta.org/myredirect.html -o /dev/null -w '%{url_effective}'

Let’s break down what this is doing:

curl -s -L -D – http://test.chrislatta.org/myredirect.html -o /dev/null -w ‘%{url_effective}’
command name Silent mode Follow redirects Dump headers here URL remove extra stdout info final destination

The output

When we run the command on our target and destination, this is what we see.

$ curl -sLD - http://test.chrislatta.org/myredirect.html -o /dev/null -w '%{url_effective}'
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=iso-8859-1
Content-Length: 244
Connection: keep-alive
Location: http://test.chrislatta.org/hop1.html

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=iso-8859-1
Content-Length: 244
Connection: keep-alive
Location: http://test.chrislatta.org/hop2.html

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=iso-8859-1
Content-Length: 244
Connection: keep-alive
Location: http://test.chrislatta.org/lost.html

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 287
Connection: keep-alive
Host-Header: 192fc2e7e50945beb8231a492d6a8024
Accept-Ranges: bytes

This output shows that that there are three redirects, or “hops”, occurring:

  1. http://test.chrislatta.org/hop1.html
  2. http://test.chrislatta.org/hop2.html
  3. http://test.chrislatta.org/lost.html

So even though the target is ultimately redirecting to the intended destination, there are two extra hops that need to be eliminated. We want to remove these for both SEO purposes and also just in the interest of keeping the system configuration clean.

Correcting the extra hops

In this case, a search through the Apache configuration or .htaccess file for mod_rewrite rules related to the undesirable redirects will lead us to the source of the problem.

$ grep -e '/hop1.html' -e '/hop2.html' -e '' .htaccess
RewriteRule ^myredirect\.html$ /hop1.html [R=301]
RewriteRule ^hop1\.html$ /hop2.html [NC,R=301]
RewriteRule ^hop2\.html$ /lost.html [NC,R=301]

Remove all of these and replace it with a single rule the redirects or target to the destination.

RewriteRule ^myredirect\.html$ /lost.html [R=301]