Skip to content

Instantly share code, notes, and snippets.

@badfeather
Last active June 8, 2021 18:58
Show Gist options
  • Save badfeather/3f0d5c347fe0ac7a4174 to your computer and use it in GitHub Desktop.
Save badfeather/3f0d5c347fe0ac7a4174 to your computer and use it in GitHub Desktop.
Allow nested folder paths in WordPress Multisite with subdirectories
<?php
/**
* Allow nested folder paths in WordPress Multisite with subdirectories
* Updated from http://maisonbisson.com/post/14052/wordpress-hacks-nested-paths-for-wpmu-blogs/ to account for latest WordPress code
* .htaccess rules also need to be changed to:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([[_0-9a-zA-Z-]+/]*)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([[_0-9a-zA-Z-]+/]*)?(wp-(content|admin|includes)/.*) $2 [L]
RewriteRule ^([[_0-9a-zA-Z-]+/]*)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
*/
if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
$current_site = new stdClass;
$current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
$current_site->domain = $domain = DOMAIN_CURRENT_SITE;
$current_site->path = $path = PATH_CURRENT_SITE;
if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
$current_site->blog_id = BLOG_ID_CURRENT_SITE;
} elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
$current_site->blog_id = BLOGID_CURRENT_SITE;
}
$url = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$patharray = ( array ) explode( '/', trim( $url, '/' ) );
$pathsearch = '';
$blogsearch = '';
if ( count( $patharray ) ) {
foreach( $patharray as $pathpart ) {
$pathsearch .= '/' . $pathpart;
$blogsearch .= $wpdb->prepare( " OR (domain = %s AND path = %s) ", $domain, $pathsearch . '/' );
}
}
$current_blog = $wpdb->get_row( $wpdb->prepare( "SELECT *, LENGTH( path ) as pathlen FROM $wpdb->blogs WHERE domain = %s AND path = '/'", $domain ) . $blogsearch . 'ORDER BY pathlen DESC LIMIT 1' );
$blog_id = $current_blog->blog_id;
$public = $current_blog->public;
$site_id = $current_blog->site_id;
$current_site = _ss_get_current_site_name( $current_site );
}
function _ss_get_current_site_name( $current_site ) {
global $wpdb;
$current_site->site_name = wp_cache_get( $current_site->id . ':current_site_name', "site-options" );
if ( !$current_site->site_name ) {
$current_site->site_name = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key = 'site_name'", $current_site->id ) );
if ( $current_site->site_name == null ) {
$current_site->site_name = ucfirst( $current_site->domain );
}
wp_cache_set( $current_site->id . ':current_site_name', $current_site->site_name, 'site-options' );
}
return $current_site;
}
@nankers27
Copy link

Hi,

Thanks for putting this code up.

I've been struggling with this for a couple of days now and have been fairly close to having it work but always with one issue or another. I feel with your code I'm very close. I've edited my .htaacess to match yours and changed my sunrise.php to match also. I assume it needed a ?> at the end to close the php tag.

I can access my main site and second site which is nested. e.g.
main site: mydomain.com
second site: mydomain.com/projects/site2

I can also access my wp-admin for main site and network but not for site 2. I get the following error in safari:

Safari can't open the page.
Too many redirects occurred trying to open my domain.com/projects/site2/wp-admin/
This might occur if you open a page that is redirected to open another page which is then redirected to open the original page.

Any ideas where I might be going wrong? Did you wp-admin work for your nested sites?

I thought I'd add another site to see if it was caused by adding site2 before making the changes, however, if I go and try and add my domain.com/projects/site2 it doesn't like this at all and throws up missing or invalid site address when I try and create the site.

Do you have to create the sites unnested and then change the url in database manually? If so, then I'm assuming that the only issue is that wordpress doesn't know where to find the admin for the site2 because the domain path has changed. Surely, this can be updated in one of the databases to point it to the correct location....I just don't know where.

Thanks,
Heath

@Falc
Copy link

Falc commented May 4, 2016

We found the same problem @nankers27 described, but a simple tweak in the .htaccess fixed everything. This is our .htaccess:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([[_0-9a-zA-Z-]+/]*)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.+)?/(wp-(includes|admin|content)(/.*)?) $2 [L]
RewriteRule ^([[_0-9a-zA-Z-]+/]*)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

Thank you for pointing us in the right direction.

@adamsposto
Copy link

I've applied this solution to add sub-directories to multi-site child sites and see an issue of the child site dashboard pulling the home /base theme dashboard instead (while showing the correct /site/directory1/directory2/wp-admin URL) – thoughts?

@richardmax
Copy link

@adamsposto Did you manage to fix this? Same issue here! All works but wp-admin is always showing the root domain wp-admin.

@badfeather
Copy link
Author

Note: Just updated the code to get rid of a pesky wpdb:prepare() notice about using the incorrect number of arguments

@davejtoews
Copy link

Just noting that as @nanker27 pointed out, you do get an error when trying to create a site with a nested subdirectory. Missing or invalid site address.

Looking at the relevant code in the WP core, there's probably no good way of fixing this, as you'd need to somehow bypass old procedural PHP code. The simple workaround is just to create the page at a temporary url and then add the nested URL in by editing it after the fact. No need to manually edit in the DB.

@Falc's .htaccess fix is still working in 2018. The relevant bit being the following line:

RewriteRule ^(.+)?/(wp-(includes|admin|content)(/.*)?) $2 [L]

@phalkmin
Copy link

Hi everyone, I'm trying this solution but every time all css/js/images from wp-content just return 404. Have someone figured out how to solve this?

@giulianopires
Copy link

@phalkmin same problem here. did you solve?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment