Last active
August 25, 2023 06:55
-
-
Save imCorfitz/7bb3688af5006965e75a3fb9885dd30e to your computer and use it in GitHub Desktop.
Allow for nested subdirectories in WordPress Multisite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BEGIN WordPress Multisite | |
# Using subfolder network type: https://wordpress.org/documentation/article/htaccess/#multisite | |
RewriteEngine On | |
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] | |
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] | |
# We are changing this rewrite rule to allow any content to appear before wp-[directories]. | |
# WordPress will take of the rest internally. | |
# RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L] | |
RewriteRule ^(.+)?/(wp-(content|admin|includes).*) $2 [L] | |
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L] | |
RewriteRule . index.php [L] | |
# END WordPress Multisite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** wp-content/sunrise.php **/ | |
if (defined('DOMAIN_CURRENT_SITE') && defined('PATH_CURRENT_SITE')) { | |
$current_site = new stdClass(); | |
$current_site->id = (defined('SITE_ID_CURRENT_SITE') ? constant('SITE_ID_CURRENT_SITE') : 1); | |
$current_site->domain = $domain = DOMAIN_CURRENT_SITE; | |
$current_site->path = $path = PATH_CURRENT_SITE; | |
if (defined('BLOGID_CURRENT_SITE')) | |
$current_site->blog_id = constant('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, $path) . $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 = corfitz_get_current_site_name($current_site); | |
} | |
function corfitz_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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ... rest of config | |
// Activate sunrise script | |
define('SUNRISE', TRUE); | |
/* That's all, stop editing! Happy publishing. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment