1

I have a multisite with domains:

site1.com, site2.com, site3.com, etc. where site.com is the "home domain" of the multisite. As of now there is two issues I am trying to solve at the same time.

If I try example.site2.com (a subdomain that does not exist) it will redirect to site1.com/wp-signup.php (and i have registration disabled). I tried to fix this with https://wordpress.stackexchange.com/a/85530/35677, with mine being:

function prevent_multisite_signup() 
{
    wp_redirect(site_url(), 301);
    exit;
}
add_action('signup_header', 'prevent_multisite_signup');

Of course this does do partially what I want which is to redirect to the homepage with a 301, however it is site1.com and not site2.com. Is there a way to adjust this code to always direct to its parent domain? I even tried getting the blog_id first, then get that url and pass it, but by the time this function is called the blog id is for site1.com.

The ideal answer would be in code versus something like .htaccess.

2
  • You could consider using a .htaccess file.
    – kaiser
    Jan 17, 2014 at 5:07
  • @kaiser Is there a way to redefine the signup_header using apply_filters('wp_signup_location', ....) and then do the redirect I want in there?
    – Shawn
    Jan 17, 2014 at 17:45

2 Answers 2

1

If you want all unregistered subdomains to redirect to the main domain site:

function prevent_multisite_signup() 
{
    $domain = $_SERVER['HTTP_HOST'];
    $domain = explode('.', $domain);
    $domain = array_reverse($domain);
    $domain = "$domain[1].$domain[0]";
    wp_redirect($domain, 301);
    exit;
}
add_action('signup_header', 'prevent_multisite_signup');
3
  • This is not what I am looking to do, as the code I posted does this. What I want is junk.site2.com to redirect to site2.com OR junk.site3.com to go to site3.com EVEN IF site1.com is technically the "main site" in multisite.
    – Shawn
    Jan 17, 2014 at 9:05
  • Edited my answer. This should work for 2 part TLDs. You would need something much fancier if you wanted to support 2 and 3 part TLDs. Also this assumes that you have configured for subdomain wildcards in DNS.
    – user42826
    Jan 17, 2014 at 15:03
  • This doesn't work because by the time wordpress executes signup_header it has already redirected the user back to the "main domain" looking for the signup page. Thus HTTP_HOST is site1.com. I am looking to see if i can either redefine the location of signup.php thats in canonical.php OR if there is another way to get up higher in the code stack.
    – Shawn
    Jan 17, 2014 at 17:42
1

I was able to solve this without .htaccess.

In my wp-config.php I retrieved the sites in an array. You can get this from your database if you need to. I then preg-matched the url being entered and checked against the known sites. In my case all urls end with .com (I could always modify the preg if need be in the future for .net or w/e). I also sanitize the url just in case for security. I then basically tell wordpress when it goes to redirect to use the base domain (i.e the homepage of the accessed domain). The wordpress default is to go to the main site in your multisite install.

Here is my snippet:

preg_match('#^.*\.([A-Za-z]*.com)#', filter_var($_SERVER["HTTP_HOST"], FILTER_SANITIZE_URL), $match);
$attempted_domain = $match[1];
$domain = 'http://site1.com';

//wordpress handles the parent domain of multisite special; so DO NOT set NOBLOGREDIRECT for site1.com. For example site1.com/doesnotexist will not show a 404 page but the homepage instead which is incorrect so have to add this first if conditional
if($attempted_domain == $domain){
    //otherwise check against our domain list and then redirect to that domain (site2.com/thisdoesnot exist will work correctly)
    foreach($all_domains AS $known_domain){
        if($attempted_domain == $known_domain){
            $domain = 'http://'.$known_domain;
            break;
        }
    }
    define('NOBLOGREDIRECT', $domain);
}

1.) What's subtle to realize here is that DNS resolves your domain. So something like blah.awesome.crazy.foo.bar.site1.com will come to your site. In this case I am making sure that no matter how many sub domain levels exist I am checking against the base domain site1.com in my list of sites.

2.) NOBLOGREDIRECT will only be used if you attempt to go to a non-existent site. Missing pages/posts for example will still 404 as they normally would. See #4 for exception

3.) If for some reason the domain is not found use a default that we know exists. This could happen if for example we changed our DNS but didn't actually setup the site yet.

4.) There is a special case around internal wordpress functionality and NOBLOGREDIRECT when accessing the parent domain of the install. You'll notice that I don't ever set NOBLOGREDIRECT when the parent domain is the domain being accessed. Otherwise 404's are not shown for the parent, you get redirected to the homepage which is incorrect. Other domains on the install are not affected so we can use NOBLOGREDIRECT there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.