Rewrite WordPress Multisites

Recently, one of my tasks was to create a Blog and a Knowledge Base and use similar credentials and login-flows for both of their Admin Consoles. Without a doubt, WordPress Multisite is the perfect choice for this since you don’t have to manage your Auth/Settings/Dashboard in multiple places.

Note: Creating WordPress multisites is out of the scope of this article. For more info, check out this link.

When you set up a WordPress Multisite install, you create one root site and then all other multisites are created as subfolders (children). For example, you may end up with something like:

http://localhost/blog/ # Root Site
http://localhost/blog/knowledgebase/ # Multisite 1
http://localhost/blog/site/ # Multisite 2

As you can see, this is not ideal, you would ideally want something like this, i.e. each multisite at the same level as the root site (blog):


It turns out – you can easily do this in 3 steps:

Step 1: Create a root level .htaccess

We need to intercept all request to multisites and then rewrite them to their original URLs.

Please note that this is NOT a redirect, but a rewrite. There’s a difference.

Moving along – create an .htaccess file in the root. If you’re using something like XAMPP, create it in the htdocs folder:

# Root Level .htaccess

RewriteEngine On
RewriteBase /

# Redirect knowledgebase to knowledgebase/
RewriteRule knowledgebase$ knowledgebase/ [R=301,L]

# Intercept all requests coming to http://localhost/knowledgebase
RewriteCond %{REQUEST_URI} /knowledgebase

# Rewrite the intercepted requests to
# /knowledgebase/anything-here from /blog/knowledgebase/anything-here
RewriteRule ^knowledgebase/(.*) blog/knowledgebase/$1 [L]

Step 2: Update the Database

Just rewriting in the .htaccess is not going to cut it for us. There are some entries in the Database that we need to change:

-- Update wp_blogs table to remove /blog from the path
UPDATE wp_blogs SET path="/knowledgebase/" WHERE path="/blog/knowledgebase/";

-- Update wp_SITE_ID_options siteurl to remove /blog in the URL
UPDATE wp_2_options SET option_value="http://localhost/knowledgebase" WHERE option_name="siteurl";

-- Update wp_SITE_ID_options home to remove /blog in the URL
UPDATE wp_2_options SET option_value="http://localhost/knowledgebase" WHERE option_name="home";

Step 3: Handle cookies/sessions

Now, we need to handle cookies/sessions since our URLs have changed. Paste the following lines in the wp-config.php file below the multisite lines:

define('ADMIN_COOKIE_PATH', '/');
define('COOKIE_DOMAIN', '');
define('COOKIEPATH', '');
define('SITECOOKIEPATH', '');

And we’re done! Visit your new multisite URL: http://localhost/knowledgebase and you should be good to go!

Note: For adding more multisites, add a necessary rule in Step 1 and update the necessary tables in Step 2.