• Hi! I have a multisite with sub-domains. I have several TLDs with DNS A Records pointing to the mainsite.com URL. These TLDs are added to cPanel of mainsite.com as Addon domains. In WordPress Network Admin, I have added TLD domain1.com to Site Address (URL) for subsite2.site.com. Redirects perfectly. Now I want TLDs domain2.com and domain3.com to also redirect and resolve on domain1.com (or subsite2.site.com which redirects to domain1.com – however it needs to be set up). I’ve looked and looked and can’t find a solution. Any ideas? Thank you!!

    P.S. Since the domains are Addon, using cPanel LetsEncrypt auto SSL I am able to get a secure lock on both subsite2.site.com and domain1.com. Would like to do the same for domain2.com and domain3.com resolving on domain1.com.

    • This topic was modified 4 years, 1 month ago by Chuck.
    • This topic was modified 4 years, 1 month ago by Chuck.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz

    (@bcworkz)

    I know this will work on single sites, not so sure about networks. It’s easy enough to try and see. See the portion about dynamically setting the constant in the WP_SITEURL section. Same goes for the subsequent blog address section. This should allow the SSL certs to work correctly.

    Keep a backup copy of the current wp-config.php as a fallback in case this doesn’t work as expected.

    Dons the Capt. Pedantic hat — “TLD” (top level domain) refers to the .com portion (or .org, .edu, .gov, etc.). You apparently just have several domain names, not TLDs. FYI ??

    Thread Starter Chuck

    (@chuckstr)

    Thank you for your response! Anyway you could give me example code? The linked instructions aren’t clear. The first domain (domain1.com) is mapped (to https://subsite.mymultisite.com) using the WordPress internal “site” settings. And works fine. But what do I put in the wp-config file for the other two (domain2.com and domain3.com) to map them to subsite (https://subsite.mymultisite.com) resolving to domain1.com?

    There is so little info available on the web regarding this topic, your assistance would be most appreciated!! Thank you!

    Moderator bcworkz

    (@bcworkz)

    If your WP installation is in public root (I think it must be for domain mapped multisite) use this:

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

    $_SERVER[‘HTTP_HOST’] contains the requested domain, so it doesn’t matter here what your actual domains are. If someone requested the wrong domain they’d never get to this point because of DNS routing. Any of your valid domains will work. Reminder: untested on multisite, but I think it’ll work.

    Thread Starter Chuck

    (@chuckstr)

    Thank you! Doesn’t seem to work though. My multisite is on a shared IP. Would making it a static IP make a difference?

    Moderator bcworkz

    (@bcworkz)

    No, IPs are not a factor. Doesn’t work in what way? SSL will not validate? Wrong domain appears? Server error?

    Thread Starter Chuck

    (@chuckstr)

    Sorry I wasn’t more specific. Domain2.com and Domain3.com do not resolve to
    https://subsite.mymultisite.com. They are resolving to mymultisite.com as a new blog sign-up as domain2com.mymultisite.com and domain3com.mymultisite.com.

    Thread Starter Chuck

    (@chuckstr)

    Correction: They are resolving to mymultisite.com as a new blog POSSIBLE sign-up as domain2com.mymultisite.com and domain3com.mymultisite.com. The multisite does not automatically create new blogs.

    Moderator bcworkz

    (@bcworkz)

    Oh! I hadn’t expected that behavior. WP is by default setup to manage one domain per site and no more. To allow WP to respond to more than one domain, we have to tell it what site we want for the added domains. This can be done through the ‘pre_get_site_by_path’ filter. The requested domain is passed as the second parameter. If it’s one of the added domains, get the desired site object and return it from the filter callback.

    At least that’s what it looks like in studying the network startup code. Completely untested, but something like this added to your theme’s functions.php:

    add_filter('pre_get_site_by_path', function( $site, $domain, $path, $segments, $paths ){
       if ( in_array( $domain, ['Domain2.com','Domain3.com',])) {
          $site = get_sites(['ID'=> 1,]);
       }
       return $site;
    }, 10, 5 );

    Modify the domain names and site ID to suit your actual situation.

    Thread Starter Chuck

    (@chuckstr)

    Good idea! Cleared cache. I added the code to the subsite’s child theme. Changed the domains. No luck. I changed the ID from 1 to the subsite’s ID. Still no. I deleted the code and added it to the mainsite child theme. Still no. I changed the site ID back to 1. No luck. Sorry!

    Moderator bcworkz

    (@bcworkz)

    That’s disappointing. The approach seemed so promising. I thought there would be a way to dynamically alter what domain WP thinks it belongs to. Perhaps there is, but I don’t see it. In parts of the code where it might be feasible, there are no filters we can hook into. There seems to be an impasse to achieving our goal.

    Of course we can redirect other domains to the primary domain, but I don’t see a good way to get the site’s address appear as whatever was requested. Sorry to take you down a dead end path, but I’m out of ideas.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Redirect multiple TLDs to one sub-domain.’ is closed to new replies.