WordPress multisite with a mix of subdomains and subdirectories

While setting up multisite, you decide to use either subdomains or subdirectories — mixing both in one multisite is not possible without modifications. This post shows you how to run a multisite with subdomains and subdirectory sites.

I recently moved my site from .de to .com and the English version from en.florianbrinkmann.com to florianbrinkmann.com/en. The problem: besides the English and German site, the demos of my WordPress themes are also part of the multisite, and they should stay on their subdomains. So I needed a mix from subdomains and subdirectory in one multisite.

Luckily I am not the first with this problem, and while searching for a solution, I found the posts »WordPress Multisite: (Sub-)Domains und Sub-Directories kombinieren« by David Naber and »WordPress Multisite mit mehreren Domains einrichten« by Thomas Scholz, which were very helpful.

These are the steps I made for changing the structure of my site (starting from a multisite which is set up for subdomains):

  1. Inserting define( 'NOBLOGREDIRECT', 'https://example.com' ); into wp-config.php to disable registration of new sites via the frontend. Perhaps you need to write remove_action( 'template_redirect', 'maybe_redirect_404' ); into the theme’s functions.php or (better) into a (MU) plugin, to prevent the site from redirecting 404 errors to the main domain.
  2. Defining the following constants in wp-config.php:
    define( 'ADMIN_COOKIE_PATH', '/' );
    define( 'COOKIEPATH', '' );
    define( 'SITECOOKIEPATH', '' );

    Without this code, you would have to log in into the subdirectory site(s), also if you are already logged in into the main site or a subdomain site. The starting point for this solution were the linked articles and an answer in the WordPress.org forum.

  3. Modify WordPress’ rewrite rules inside the .htaccess, so they match the rules of subdirectory installations, to let you access the backend of your subdirectory install:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    # add a trailing slash to /wp-admin
    RewriteRule ^wp-admin$ wp-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]
  4. Create a new site in your multisite (it does not matter which subdomain you choose).
  5. Open the site’s information and enter the main domain with the subdirectory. Until a few WordPress versions, there was an extra field for the path.
  6. Done. 🎉

If you are switching a site from a subdomain to a subdirectory like I did, remember to create redirects! You can find example code for redirecting a subdomain to a subdirectory on Stack Overflow.

Related posts

2 comments on »WordPress multisite with a mix of subdomains and subdirectories«

  1. Rob

    Will this allow you to keep all of the current subdomain name blogs? And will this switch all new registrations to subdirectory?

    Also, will it prevent new sign ups on the front end?

    Thanks

    Reply
    1. Florian Brinkmann

      Hi Rob,

      sorry for the late reply!

      »Will this allow you to keep all of the current subdomain name blogs? And will this switch all new registrations to subdirectory?«

      This will not remove any of the existing sites of the multisite, and will not change the default registration from subdomain to subdirectory.

      »Also, will it prevent new sign ups on the front end?«

      Yes, the first step does this.

      Cheers,
      Florian

      Reply

Leave a Reply to Florian Brinkmann Cancel reply

Your email address will not be published. Required fields are marked *