[wpseo_breadcrumb]
Nginx

Nginx – Double Redirect Problem

Nginx double redirect problem:

Quickly writing this to help out anybody who is trying to fix a double redirect on their Nginx site for backlinks coming in on non-www on an http protocol.

On a site that is using Cloudflare as a CDN.

You may be scratching your head when looking at the results of site audit tools saying there is a double redirect problem from;

http://example.com to https://example.com

And then;

https://example.com to https://www.example.com

And you stick it into a redirect checker to confirm and discover it to be the case;

Ahrefs Audit

Which left me wondering what is wrong. As per this answer I had Nginx and Let’s Encrypt set up to redirect http://example.com to https://www.example.com in a single hop;

server {
  listen 80;
  server_name example.com;
  return 301 https://www.example.company$request_uri;
}

server {
  listen 443 ssl;
  server_name www.example.com;
  ssl_certificate /etc/letsencrypt/live/www.site.com/fullchain.pem;     
  ssl_certificate_key /etc/letsencrypt/live/www.site.com/privkey.pem;
  # your location there a
}

So how is that happening? As you’ve probably guessed based on the bold second sentence, Cloudflare was the culprit. Although to be fair, ‘culprit’ is pretty unfair as it’s just a config setting they offer which was applied when it didn’t need to be.

It’s one of those settings you can easily turn on when you setup a site initially thinking it to be a good thing. If you are handling this at a server level it can subsequently be found and disabled;

SSL/TLS -> Edge Certificates -> Always use HTTPS

Cloudflare automatic redirect

Hey presto, a single redirect and the SEO guy is happy again. For a few minutes …