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):
- Inserting
define( 'NOBLOGREDIRECT', 'https://example.com' );
intowp-config.php
to disable registration of new sites via the frontend. Perhaps you need to writeremove_action( 'template_redirect', 'maybe_redirect_404' );
into the theme’sfunctions.php
or (better) into a (MU) plugin, to prevent the site from redirecting 404 errors to the main domain. - 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.
- 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]
- Create a new site in your multisite (it does not matter which subdomain you choose).
- 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.
- 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.
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
Hi Rob,
sorry for the late reply!
This will not remove any of the existing sites of the multisite, and will not change the default registration from subdomain to subdirectory.
Yes, the first step does this.
Cheers,
Florian
Hello Florian,
I am facing a real problem with multisite. Below is the problem I have mentioned.
https://wordpress.stackexchange.com/questions/289214/how-to-connect-existing-subdomains-to-new-wordpress-multisite
Can you please help?
Hi Parth,
I am sorry but I think I cannot really help you with the problem, I hope you can fix it with the tips from David.
Cheers,
Florian
Hi Parth,
thank you very much for sharing this information. You saved me a ton of time!! I was in the same situation as you.
This information helped me too. Thank you very much.
Still works, lifesaver.