Single WordPress Instance with Multiple Domains

Add the following code to your wp-config.php file, right before the line that says /* That's all, stop editing! Happy publishing. */

$domain = array("domain1.com", "www.domain1.com", "domain2.com", "www.domain2.com");
if (in_array($_SERVER['HTTP_HOST'], $domain)) {
    define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
    define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
}

This code dynamically sets the site URL based on how a visitor accesses the site, ensuring both domains work correctly.

Important Considerations:

  • Allow All Domains: If you want to allow any domain pointing to your server, replace the if (in_array(...)) block with just the two define lines, though using the domain array is safer to prevent unwanted access.

  • HTTPS Support: If your site uses HTTPS, change 'http://' to 'https://' in both define statements.

  • Subdirectory Installations: If WordPress is in a subdirectory (e.g., /wp), modify the code accordingly: define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wp');.

  • Avoid Plugin Conflicts: When testing, temporarily deactivate plugins to check for conflicts.