Fixing redirection problems with cURL


A fairly frequent problem I often encounter at Codeable are redirection errors, caused by plugins, incorrect .htaccess file settings or coming from Cloudflare, or even from javascript; sometimes it’s a combination of several of these factors.

To find the causes of these problems, the first thing we have to do is to look at these redirects, where they take us and the headers of the results. But if we put the URL in the browser, what we will see in most cases is the final URL, so we will use cURL.

cURL is an essential utility for every web developer, which can be used for a multitude of tasks, from obtaining data from an external API to Debugging web request headers.

In my case, using Windows 10, I use WSL (with Ubuntu 18.04) which is already part of my daily work since they announced their beta. The command to use is:

curl -s -L --max-redirs 5 -D - -o /dev/null  -w '%{url_effective}'

First we type the command curl followed by the following parameters:

  • -s to enable silent mode.
  • -L to follow the redirections.
  • --max-redirs X to indicate the number of redirects to follow (indicated by X) since by default, if we do not put anything, it would follow 50 redirects and in the loop redirects with a much lower number is enough.
  • -D - to perform a dump of the headers here.
  • the URL we want to inspect.
  • -o /dev/null redirect the output to /dev/nulli.e. we do not show the output which would be the resulting html page.
  • -w '%{url_effective}'rewrite the output of the headers to the screen after hiding the rest of the output.

And the result we get is something similar to the following in which we can see that only a redirection has been made to move from the http version to the https version and also change the www version to the canonical version without www.

curl detecting redirects

Now we can start investigating depending on whether the redirects come from WordPress, CloudFlare or another CDN or external tool, if it is done by javascript once the HTML is loaded, etc.

And if you have any questions about curl, you already know that you have its manual with man curl or online from

WordPress redirects

But in addition to the redirects that we can have from Apache or Nginx, these can also come from WordPress made through wp_redirect or wp_safe_redirect that will carry the X-Redirect-By header usually with the WordPress value, although this value can be another declared by a plugin or custom.

To avoid some redirects while DEBUGging our code, we can remove problematic redirects with the following code in the functions.php of our active theme:

remove_action( 'template_redirect', 'redirect_canonical' );

Leave a Comment