Skip to content

Instantly share code, notes, and snippets.

@colegeissinger
Last active December 7, 2022 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colegeissinger/b7d98e53997812e7f82113721e9f0a74 to your computer and use it in GitHub Desktop.
Save colegeissinger/b7d98e53997812e7f82113721e9f0a74 to your computer and use it in GitHub Desktop.
How to enable multilevel directory WordPress multisite install. You Multisite should be configured with subdirectories rather than subdomains as the default.
# This should be used over the server.conf if you are running on Apache and need .htaccess rewrites. Ref this documentation for more details https://wordpress.org/support/article/htaccess/#wordpress-3-5-and-up
RewriteRule ^(/[_0-9A-Za-z-]+\/?)+(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^(/[_0-9A-Za-z-]+\/?)+(.*\.php)$ $2 [L]
# Below is an update to your NGINX rules. If you need .htaccess rewrites, use that file example in this gist. Ref this docuemntation for more details https://wordpress.org/support/article/nginx/#wordpress-multisite-subdirectory-rules
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^(/[_0-9A-Za-z-]+\/?)+(/wp-.*) $2 last;
rewrite ^(/[_0-9A-Za-z-]+\/?)+(/.*\.php) $2 last;
}
<?php
// Add the sunrise.php file to the root of your wp-content directory.
/**
* Ensure WordPres will check deeper levels of directories. By default $num_segments is null,
* This function will filter that to ensure we can account for deeper nested paths.
*
* E.g. if a site is 5 directories deep www.mysite.com/some/url/that/goes/deep/ you can set the number to 5.
* This also applies to subdomained sites. subdomain.mysite.com/some/url/that/goes/deep/
*
* @param int|null $num_segments Variable isn't returned or modified directly.
* @return int
*/
function cg_site_by_path_segments_count( $num_segments ): int {
return 5;
}
add_filter( 'site_by_path_segments_count', 'cg_site_by_path_segments_count', 99 );
<?php
...
// Add the below code to your wp-config.php, ideally after all the multisite configurations and before the "That's all, stop editing!".
define( 'SUNRISE', true );
define( 'COOKIE_DOMAIN', $_SERVER['HTTP_HOST'] );
@colegeissinger
Copy link
Author

NOTE: I included server rewrites for both Nginx and Apache. Use which ever configuration your server is using. Also note, the .htaccess hasn't been tested yet, so use with caution as it may need some additional tweaks.

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