Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmhendricks/682a0407b9950673d7aba8f991257a22 to your computer and use it in GitHub Desktop.
Save dmhendricks/682a0407b9950673d7aba8f991257a22 to your computer and use it in GitHub Desktop.
A short guide on how to configure WordPress to use/observe multiple domains pointed to a single instance.

Multiple Domains Pointing to Single WordPress Instance

Scenario

Let's say that you want to point multiple domain names to the same WordPress instance. Normally, if you do this, WordPress will siimply redirect to the home URL in the database as configured in WP Admin > Settings > General > Site Address (URL).

Why Would You Want To Do This?

There may be a case where you want multiple domains pointing to the same site - perhaps for referral tracking, to migrate an old domain, to allow the use of short URL (for example, my-really-long-domain.com and shrturl.co), etc without using a redirect.

⚠️ NB: If you point multiple domains to a single site, search engines may consider it as duplicate content at multiple domains and incur an SEO penalty, so you'll either want to add a canonical tag to your page header, else configure your site to add NOINDEX for all domains that don't match the primary.

Configuring WordPress

To allow a WordPress site to be served from multiple domains, you can modify your wp-config.php by adding (or modifying) as follows:

define( 'X_REQUEST_HOST', ( $_SERVER['HTTPS'] ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] );

define( 'WP_SITEURL', X_REQUEST_HOST );
define( 'WP_HOME', X_REQUEST_HOST );

This will tell WordPress to use whichever domain that is being used to access it. If you wish to always force HTTPS, you could change the first line to simply:

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

DNS Notes/Setup

It should be noted that before you can access your site from multiple domains, you must configure your DNS to point the multiple domains to resolve to your web site. You might not be able to do this with some shared hosting providers, depending on their setup (else they may have a different way of pointing multiple domains in their control panel).

Doing this varies by DNS provider (examples: GoDaddy, Cloudflare, NameCheap, etc). It is beyond the scope of this article to describe how to do it for each DNS provider, but in general, you would do something like this:

  1. You should already have an A record was (and possibly CNAME) for your primary domain (examples: example.com and www.example.com). This usually points to your web host's IP address.
  2. If you wish to point/alias another domain to this site site, you could add one or more CNAME records to your DNS zone.

So let's say that we wanted the short domain exmpl.co to point to your example.com WordPress install. Your DNS zone configuration might look something like this:

Record Type Name Value
A example.com 123.45.67.89
CNAME www.example.com example.com
CNAME exmpl.co example.com
CNAME www.exmpl.co example.com
CNAME somethingelse.com example.com

The above example would point www.example.com, exmpl.co, www.exmpl.co and somethingelse.com to your primary domain, example.com (which is configured to point to the IP address of your web host). If the IP address changes for the A record, it would apply to the CNAME records as well because they are aliases.

If you're confused or not sure how to do this, it is best to ask your support for your provider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment