How to exclude subdomains from WordPress Multisite

Published December 29, 2020; last updated on January 4, 2021
How to exclude subdomains from WordPress Multisite

I love WordPress Multisite. It allows you to get a new WordPress site up and running within minutes. Especially if you’re a developer and want to test a new theme (version) on a plain installation, this is really handy. Or if you are a company and you need a new site, e.g. for a project.

WordPress Multisite works with both subfolders and subdomains. Subfolders are sites created at domain.com/newsiteslug and subdomains are subdomain.domain.com. Setting up a multisite is – generally spoken – easy, though the subdomain version is a little bit more tricky.

Excluding a subdomain from WordPress Multisite only requires a simple RewriteRule in the .htaccess file.

How to exclude a subdomain from WordPress Multisite

Open up your .htaccess file. Since you already have a Multisite installation, the .htaccess should be in your main site folder, because WordPress Multisite requires it. If you open it, you can add a custom rewrite rule.

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP_HOST} subdomain.ralphjsmit.com
RewriteCond %{REQUEST_URI} !subdomain/
RewriteRule ^(.*)$ subdomain/$1 [L]

</IfModule>

All you need to do is adding a simple snippet of code, before the other WordPress RewriteRules apply. Here’s an example of excluding two (fictional) sites subdomain.ralphjsmit.com and subdomain2.ralphjsmit.com from WordPress Multisite. Make sure to place it before the WordPress part.

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP_HOST} subdomain.ralphjsmit.com
RewriteCond %{REQUEST_URI} !subdomain/
RewriteRule ^(.*)$ subdomain/$1 [L]

RewriteCond %{HTTP_HOST} subdomain2.ralphjsmit.com
RewriteCond %{REQUEST_URI} !subdomain2/
RewriteRule ^(.*)$ subdomain2/$1 [L]

</IfModule>

Excluding a subdomain is really as simple as this! Remember to update the reference in all three lines per subdomain, otherwise things might not work as expected.

Stay up to date with all things Laravel, Tailwind, WordPress & PHP

Subscribe now to my e-mail newsletter and get my latest articles and project updates delivered directly to your inbox. Never miss an update.

Image Ralph J. Smit
Ralph is a designer gone developer. He happily lives in the Netherlands. His passion for good design drove him towards development, because he felt that no-code tools were too limiting. On this blog, Ralph writes the articles he would've wanted to have during his continual developer journey. → Follow on Twitter

Comments

Leave a reply

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

Close
Do you want more articles like this delivered straight to your inbox?

Subscribe now to my e-mail newsletter and get my latest articles and project updates delivered directly to your inbox. Never miss an update.