• We have multisite set up with sub-domains (mapped to normal domains) and we do use some sub-domains (like test or dev) that aren’t part of the multisite, but I want other sub-domains (say if someone mistypes) to just redirect to our main domain.

    I have * in the DNS so it goes to our server properly, and I have *.ourdomain.com in our vhost file which handles any random sub-domain, but then it pushes it to the WordPress install which tries to treat it like an official sub-domain and gives an “Error connecting to database”. I just want it to redirect to ourdomain.com and I’m not sure the best way to do this.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Have you met my friend define( 'NOBLOGREDIRECT', '' ); ?

    define( 'NOBLOGREDIRECT', 'http://maindomain.com' );

    Enjoy 😀

    Thread Starter davehprohoods

    (@davehprohoods)

    That will work for only one domain; if they reach a non-existent sub-domain on any of the sub-sites, it will go to the main domain, which is not desirable. Any other solutions?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    but I want other sub-domains (say if someone mistypes) to just redirect to our main domain.

    That’s what you said you wanted. Now you’re saying that’s NOT what you want?

    You can wrap that define around some PHP code.

    if domain is domain1, redirect to maindomain1.com
    if domain is domain2, redirect to maindomain2.com

    So then it’ll call the noblogredirect dependant on what domain.

    OR you can have PHP check for the main domain and make a variable and use that in the define.

    Thread Starter davehprohoods

    (@davehprohoods)

    OK yes sorry I deal with multiple WordPress installs and I was hoping the solution would apply all around. In some installs we do want any non-recognized sub-domains to redirect to the main domain but on others (like multisites that use sub-domains) we want unrecognized sub-domains to redirect to that main site, not the base site it was installed under if it’s different.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    You have to give us a clear example/representation of your setup and needs if you want to have any hope for a complete answer, just FYI. I mean, I can only help what you tell me 🙂 The best solutions aren’t always applicable all around because we want to keep things as simple as possible to reduce overhead.

    So!

    In some installs we do want any non-recognized sub-domains to redirect to the main domain but on others (like multisites that use sub-domains) we want unrecognized sub-domains to redirect to that main site, not the base site it was installed under if it’s different.

    Then my second suggestion IS what you want.

    $domain = $_SERVER['SERVER_NAME'];
    $domain = str_replace('www.', '', $domain);
    if ($domain == 'seconddomain.com') {
       define( 'NOBLOGREDIRECT', 'http://seconddomain.com' );
    } elseif ($domain == 'thirddomain.com') {
       define( 'NOBLOGREDIRECT', 'http://thirddomain.com' );
    } else {
       define( 'NOBLOGREDIRECT', 'http://maindomain.com' );
    }

    So what this does is say ‘If I’m second domain, redirect there, and third domain ditto, but all else default to maindomain.’ That would cover multinetworks too 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Make non-existent sub-domains redirect to main domain’ is closed to new replies.