Map Domains to Single WordPress Site

Map Domains to Single WordPress Site

We had a client recently needing their separate, single-install of WordPress to have two domains mapped to it.  After more Googling than we care to admit, we discovered the following relatively simple solution.

First off, if you’re using WordPress Multisite, you don’t need to bother with this idea.  This is simply for single-site installs of WordPress.  For Multisite, we highly recommend using the fabulous plugin WordPress Multisite Domain Mapping from our friends at wpmudevNOTE: You do not have to buy a subscription.  You can buy just the plugin, but the subscription DOES have it’s advantages.

The Backstory

This client had a child brand site hosted elsewhere.  It was incompatible with WordPress Multisite on their parent domain, but the parent domain has hosted on a rock star server.  Rather than have their child company reside on cheap shared hosting or having to run two small sites on two different rock star web servers, JDM recommended they simply move the child site into a subdomain off the root domain hosted on that rock star server.

The problem? They didn’t want the child site to have a subdomain.  They wanted it to have it’s own domain.  Who can blame them.  Here’s what we came up with.

The Solution

In any single instance of WordPress under Settings >> General there are two fields for “WordPress Address” and “Site Address”.  These are usually the same URL.  What they wanted is the ability to access the site from BOTH its subdomain location as well as its own domain.  So we can’t change it here.  We have to get a little fancy.

The solution starts with making these fields dynamic.  There is a directive supported by WordPress allowing you to set (and override) the WordPress URL from the wp-config.php file.  All we have to do is use that directive with dynamic values, like so:

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

The dynamic part (in bold) which basically asks the server to set the site URL and home URL (those two fields mentioned above in General Settings) to whatever URL we’re at right now.

If you were to head back into the WordPress General Settings, you’d see those fields are now disabled.

wordpress-general-site-disabled

We’re half way there. Just a little DNS magic to finish it up.

  1. Setup the subdomain off the parent domain as per usual
  2. Setup the A-record in the DNS zone file for the new domain to point to the IP address of the new subdomain

Presumaby you could do this via a CNAME (alias, basically) as well.  DNS can do magical things…

Leave a Reply