WordPress 3.0 Multisite – Subdomain installation NOBLOGREDIRECT behavior fix.
A little explanation for those who have no idea what NOBLOGREDIRECT is.
The define(‘NOBLOGREDIRECT’, ‘%siteurl%’); inside of the wp-config.php makes it so that when someone enters a subdomain that does not exist on your site to redirect to whatever url you wish it to. You can use this to have it either go to a specific FAQ page or directly back to the main root installation, anywhere you want to direct it. the %siteurl% can be replaced for example define(‘NOBLOGREDIRECT’, ‘https://frumph.net/FAQ/site-create’);
When someone in their browser tries to go to (for example) http://badsubdomain.frumph.net/ a subomain which doesn’t exist, it will go to what is defined in NOBLOGREDIRECT.
Without using NOBLOGREDIRECT the (for example) http://badsubdomain.frumph.net/ – which is a subdomain that doesn’t exist would direct to the signup page asking which reports whether or not the user can create the bad subdomain in question. This is fine, there’s nothing wrong with it redirecting to the signup page if someone put in a bad url. However, those of us who think it’s rather tasteful to have the user just redirect to the home page don’t like that, so we want to use the NOBLOGREDIRECT to direct things to the front page; have the user look at the site first.
Now where’s the behavior problem? It’s when using NOBLOGREDIRECT and on your normal use of the mainsite. The bad behavior is when someone goes to a page on the normal site that doesn’t exist. For example https://frumph.net/idontexist/ ; Instead of redirecting to the sites 404 page (the page not found template) it will redirect you to what is specified in NOBLOGREDIRECT. Wait.. what? Why does it do that? The behavior of redirecting 404 pages is an old function used in WPMU that should not exist anymore however is left in when WPMU was merged with WordPress itself.
What can be done to fix the behavior so that it goes back to dishing out 404 pages? Well that’s easy, you just disable the function from being called that would redirect those 404’s.
In your /wp-content/mu-plugins/ folder, (if you don’t have one and are using multisite, you can create it manually) you add a custom.php file to it and add a line to disable the action.
For example, create a file named custom.php toss it into the /wp-content/mu-plugins/
custom.php
<?php remove_action( 'template_redirect', 'maybe_redirect_404' ); ?>
The function that is causing the bad behavior of not directing 404 pages properly is maybe_redirect_404() and it’s being called as an add_action, so in order for us to not have the function run we remove the add_action with remove_action.
Having the custom.php file (a file we created just now) placed in the mu-plugins folder means that its automatically activated and running at all times.
Now those 404 pages are redirecting to the 404 page not found template for your theme and bad subdomains entered will go to whereever you specified in the NOBLOGREDIRECT.
– Phil
NOTE: NOBLOGREDIRECT in its original intent is useful to pointing to some ‘other’ location when a signup page is required instead of sending to a 404 page, however the naming convention for the define is misleading which then leads to end-user misunderstanding of its function; although I still have no idea why anyone would *want* the signup page to show up on a 404 but obviously the multisite guys do because they still argue it’s validity.
Discussion (20) ¬
Thank you very much! That’s works perfectly!
Perfect solution to an annoying problem. Thanks for writing this up.
MM/RC
Thanks – that was just what I needed!
thank you, it really helped and worked
Thanks it helped me a lot with the redirection problem!
I’m using WP 3.1.3 in multisite mode with Mapping Domain plugin activated, I defined a custom NOBLOGREDIRECT to the main site’s home URL, and my wrong URLs look correctly redirected to the 404 page within the typed custom domain. Could be the bad behavior fixed now?
Thanks.
It’s possible, havent’ tested it yet.
tested, this is still needed in wordpress 3.2 in order to get 404 pages to properly come up
Very nice. For my purposes, I just set NOBLOGREDIRECT to my 404 page.
You sir… rock. Thanks so much for writing this up… Very annoying problem with a simple fix.
Some of you may run into the problem I did, which is in the original code you need to copy into wp-config, I needed to change the ” ` ” to ” ‘ ” in every instance.
Perfect! I’m using multisite to run multiple sites and not a hosting service for all and sundry, so I wanted NOBLOGREDIRECT but was annoyed at it overrriding my 404 errors!
Thank you very much! Works perfect. I use NOBLOGREDIRECT for redirecting the non-www to the www-domain on a mulitsite with domain mapping.
Thanks! I spent hours trying to solve this problem AND getting non-www to redirect to www. I couldn’t figure out why the rewrites in the .htaccess weren’t working correctly, and it was all due to NOBLOGREDIRECT and maybe_redirect!
Thank You Phil !
sorry to repeat the same thing, but this is brilliant, thanks a bunch!
Ya! That worked for me. Sure would be a nice feature on the Permalink Finder plugin. I will tell them 🙂