Pointing Two Domains to the Same WordPress Site

I bought a couple of domains and decided to see if I could have both of them point to the same WordPress site. This seems pretty easy to do but it required a few steps to get it done and working. I had to straighten out the site’s certificate, I had to edit some Apache config, and I had to add some code to a WordPress php file.

Step 1 – Update letsencrypt certificate

I use letsencrypt to secure the site via a TLS connection. The binary has since been updated to certbot. We’ll be using certbot on the command line to get a new updated cert that configured for multiple domains. Here’s the command I used:

sudo certbot -d danielpaluszek.com -d danielpaluszek.tech

Follow the prompts and verify the output indicates success. The new cert files should live in the letsencrypt default directory: /etc/letsencrypt/live

Step 2 – Update Apache

Now we need to configure Apache to respond to requests for my new domain via a new virtualhost configuration. We’ll copy the config file for danielpaluszek.com then configure it for danielpaluszek.tech:

cd /etc/apache2/sites-available
sudo cp danielpaluszek.com.conf danielpaluszek.tech.conf

Edit the newly created conf file, changing the ServerName directive to the new domain name. You don’t need to edit your cert file locations since we’re using one cert for both domains. Save the new file then run these commands to enable the new site then restart Apache:

sudo a2ensite danielpaluszek.tech
sudo systemctl restart apache2

Browsing to the .tech site results in success.

Part 3 – Configure WordPress for Multiple Domains

Now we need to tell WordPress that it needs to stay on the domain the web user started on. Currently if you browse to the .tech domain and click a link within the site whose destination is the site you’ll be brought to the .con domain. Lame. We can fix this by editing the wp-config.php file. Mad props to Jeevanandam M. for this. Search for the following line in the file:

$table_prefix  = 'wp_';

After $table_prefix, add the following:

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

You should now be able to browse around the site and remain on the top level domain you started on. Success.