How to fix too many redirects error in WordPress after switching to https?

Recently I found out I can use a free SSL certificate from Cloudflare. I changed the website URL in my WordPress admin settings to use Cloudflare SSL from http:// to https://. But now, I’m unable to access the website, and I’m receiving an error message saying “ERR_TOO_MANY_REDIRECTS”. How can I resolve this issue?

If the problem is with Cloudflare, then you can solve your problem just by adding the below line in your wp-config.php file.

$_SERVER['HTTPS'] = 'on';

If you plan to use SSL and non-SSL simultaneously, you can try the code below.

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
    $_SERVER['HTTPS'] = 'on';
}