Run Multiple Domains with One WordPress Instance

There may be times when you need to allow multiple domain names to work with one WordPress instance. This would allow mywebsite.com and mywebsite.net to work on the same WordPress instance and show the same website.

If you want different websites for each domain name but you want to manage all of them from one dashboard, you’ll want to learn more about WordPress Multisite. A WordPress multisite network “is a collection of sites that all share the same WordPress installation” (Source).

To run multiple domains with one WordPress instance, i.e. to associate multiple domain names with the same WordPress website, there are two options:

Option 1: Explicitly Define the Domains

I tend to prefer this method as it allows you to set the domain names explicitly. You could add the following code to your wp-config.php file:

if ($_SERVER['HTTP_HOST'] == 'www.mywebsite.com') {
    define('WP_SITEURL', 'http://www.mywebsite.com');
    define('WP_HOME',    'http://www.mywebsite.com');
} else {
    // this is the default that shows up if someone visits your site
    define('WP_SITEURL', 'http://www.mywebsite.net');
    define('WP_HOME',    'http://www.mywebsite.net');

Option 2: Dynamically Define the Domain Based on If Statement

In this case, you need to add the following code to your wp-config.php file.

if($_SERVER['HTTP_HOST'] == 'www.mywebsite.com' || $_SERVER['HTTP_host'] == 'www.mywebsite.net'){
    define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
    define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);

What this code is doing is that it’s updating your WordPress siteurl and home options dynamically each time a visitor lands on your website, as long as the HTTP_HOST value is one of the options in the if conditional. So, if I visit www.mywebsite.com, the $_SERVER[‘HTTP_HOST’] variable tells my code that a user is visiting http://www.mywebsite.com and updates the WordPress options accordingly.

You never want to use the following code without a conditional statement surrounding it. The reason for this is that a nefarious person could make your website show up on their domain name. A nefarious actor could simply update their DNS settings and point the A record to your server and then your website would show up when someone visits their domain name.

// do not use this unless you define what host names are allowed
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);

Leave a Reply

Your email address will not be published. Required fields are marked *