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).
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.
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'] );
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:
- You should already have an
A
record was (and possiblyCNAME
) for your primary domain (examples:example.com
andwww.example.com
). This usually points to your web host's IP address. - 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.