Too Many “HTTP_HOST” errors in Site Logs? Eliminate them

The solution is simple: have your wp-config.php define $SERVER[“HTTP_HOST”] if it is not set.

Many hosts don’t define this, as it isn’t needed for normal traffic. But bots, or people for some weird reason accessing your site by IP address or with a very old browser, will generate errors that clutter up your site error log.

Also, you using SSH to maintain your site in wp-cli, will often not have this set.

How to Fix It

You might be able to modify your wp-config.php file directly. Or your hosting company (especially WordPress managed hosting accounts) will require you to modify it via their user dashboard. For example, in Sites, Settings, Wp-config, “Additional wp-config.php content”.

A good place to add the code would be before this:

if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');

Or right before the common (but not always there) line with /* That's all, stop editing! Happy publishing. */

Warning: if you are not comfortable editing your wp-config.php file, or don’t know the basics of PHP code, then have your “computer friend” do this for you. Or have your hosting company do this for you.

The Code to Add

if ( defined( 'WP_CLI' ) && WP_CLI && ! isset( $_SERVER['HTTP_HOST'] ) ) {
    $_SERVER['HTTP_HOST'] = '';
}

That will prevent errors from using WP-CLI (a great tool for command line maintenance of your site).

If you want to stop errors from all sources:

if ( ! isset( $_SERVER['HTTP_HOST'] ) ) {
    $_SERVER['HTTP_HOST'] = '';
}

Note: many sites online suggest $_SERVER['HTTP_HOST'] = 'example.com'; or some other “dummy URL”, but that won’t work in wp-cli on a WordPress Multi-Site where define('DOMAIN_CURRENT_SITE', $_SERVER['HTTP_HOST'] ); tells WordPress what site to use.

That’s it, you are done.