Fix Undefined index: HTTP_X_FORWARDED_PROTO with WP-CLI

Undefined index: HTTP_X_FORWARDED_PROTO

Are you running your WordPress install behind an SSL proxy or load balancer like HAProxy? You may also be running WP-CLI on the internal site.

To get SSL to pass-through correctly a lot of sites have outlined that you need to add the following code to your wp-config.php

define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS']='on';

Although this is almost correct, the HTTP_X_FORWARDED_PROTO line is incorrect. If you use this code your WordPress site will work correctly with your SSL certificate on your proxy/load balancer. But if you try and run a WP-CLI command you’ll get the following error.

PHP Notice:  Undefined index: HTTP_X_FORWARDED_PROTO in phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1197) : eval()'d code on line 71

After some searching, I found this closed issue in the WP-CLI GitHub issues. It has a reply from Daniel Bachhuber the previous WP-CLI maintainer with an easy fix.

The Fix

The fix is pretty straight forward and all you have to do is check that the index exists and then set it as required. Updated your wp-config.php file to look like this.

define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){ $_SERVER['HTTPS']='on'; }

Once you have added the array key check it’ll work correctly both for the site itself and for wp-cli.

Share :

Related Posts

Join me as I unveil my ultimate developer workspace for 2024. Discover how I've transformed a basic setup into a cosy, productivity-boosting environment from tech to ergonomics.

I have been struggling for a couple of days working with Google Cloud Scheduler and Cloud functions for a project I’m working on. I’ve been working with functions for a while now. It’s a good idea to secure all your functions so that only other cloud services can access them. This can be done using […]