HOW TO: Set a variable HOME and SITEURL based on the requested domain

October 14, 2020

This is a method to ensure the domain part of a URI can be contextually served based on the domain the client loads your WordPress site on (ideal for reverse proxies or staging sites).

Specified method

Add this to your wp-config.php:

$domaincheck = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if (strpos($domaincheck,'https://403.ie') !== false) {
define( 'WP_HOME', 'https://403.ie' );
define( 'WP_SITEURL', 'https://403.ie') ;

} else {
define( 'WP_HOME', 'https://staging.403page.com' );
define( 'WP_SITEURL', 'https://staging.403page.com') ;
}

Global method (restricted)

Add this to your wp-config.php:

$domaincheck = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if (strpos($domaincheck,'https://403.ie') !== false) {
define( 'WP_HOME', 'https://403.ie' );
define( 'WP_SITEURL', 'https://403.ie') ;

} else {
define( 'WP_HOME', 'https://' . $_SERVER['SERVER_NAME'] );
define( 'WP_SITEURL', 'https://' . $_SERVER['SERVER_NAME'] ) ;
}

Global method (unrestricted)

Add this to your wp-config.php:

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);

Global method for reverse proxy (unrestricted)

Add this to your wp-config.php:

if ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
$_SERVER['HTTP_HOST'] = '403page.com';}
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);