Redirect WordPress Multisite Root Domain to a Subdomain

WordPress multisite is great but sometimes we need to redirect visitors from the root domain to one of the subdomains. There isn’t a plugin for this and until now, accomplishing the redirection would see you jump through hundreds of hard to follow guides that would get you beating your computer like its your worst enemy.

Questions this post answers:

  • How do I redirect from WP MS root domain to a subdomain?
  • How do I redirect like post to like post on a different sites?
  • How do I automatically send visitors from a post on one site to the same post on another site?

How to Redirect from root site to subsite

A friend of mine, Maria de Los Angeles, has a website called heartcamp.org. We recently converted her site to WordPress multisite. Maria organizes a yearly event in Miami called ‘Heartcamp’. With WP MS Maria can create a new subdomain site for each year such as 2012.heartcamp.org and 2013.heartcamp.org.

We used the excellent WPMU plugin New Blog Templates to duplicate the root site to new sites in the network as they were created.

Maria wants all visitors to heartcamp.org to be redirected to 2013.heartcamp.org

We can’t redirect using domain nameservers because the WordPress CMS that manages the network is installed in the root domain. The WordPress  root installation has all the files needed for WordPress to work and subdomain/subdirectory sites in the network are figments of WordPress’s imagination – they are created virtually by WP.

We could use the WordPress database and its wp-config file to achieve our desired result but that gets complicated.

The easiest way to create the redirect is with an htaccess redirect rule

We can redirect like-to-like such that a page on the root site redirects to its equivalent page on the subdomain site. This only works when posts (or pages) on each site have a similar URL structure that differs by the domain name only e.g 2013.example.com/page-1 and example.com/page-1; otherwise we can always redirect traffic to the homepage of the subdomain site without regard to the post or page being looked for.

So we have two methods of managing the redirect:

  1. If you’re not worried about like-to-like redirects use method one
  2. If you are worried about like-to-like redirects, use method two

Method one: redirect like-to-like

To send visitors from your mutisite’s root domain to a subdomain on the same site while maintaining slug URL structure:

  • Use FTP or your server’s cPanel file manager to open .htaccess
  • Stick these rewrite rules at the top of it
    # Block access to the root site
    RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
    # Whitelist directories and files needed by WordPress
    RewriteCond %{REQUEST_FILENAME} !/wp-.* [NC]
    RewriteCond %{REQUEST_URI} !/wp-.* [NC]
    RewriteCond %{THE_REQUEST} !/wp-.* [NC]
    # Whitelist specific areas of the root site
    # e.g category slugs or page slugs you want to remain viewable
    RewriteCond %{REQUEST_URI} !/blog.* [NC]
    RewriteCond %{THE_REQUEST} !/blog.* [NC]
    # Set like-to-like redirect URL for anything not whitelisted
    RewriteRule (.*) http://2013\.example\.com/$1 [R=301,L]
  • Change the domain names and slugs in the redirect rules so they match your site and needs
  • Save the file

Explanation

The top rule tells the server which domain we are blocking access to.

The first set of whitelist rules tell the server which URIs and files should not be blocked. All WordPress directories and files in the root directory begin ‘wp-‘ so we whitelist them to allow images, media and other items to be accessible from subdomain sites and from external sites where they might be hotlinked.

The second whitelist keeps specific directories (areas) of the site accessible. In this case we are working on the assumption a site has blog in its permalink structure for blog posts. If the new site duplicates the root site’s blog posts then there’s no need to allow access to the original blog so these lines could be removed. You can whitelist specific tag or category slugs by replacing ‘blog’ with the tag or category slug name. There’s no need to state the complete path.

The final part of the rewrite rule block tells the server where to send traffic that tried to view non whitelisted content or to visit the root site’s homepage. Notice the $1 at the end? Remove it to disable like-to-like redirects of non whitelisted pages.

How to redirect all traffic to the root site

This is easier. This time we need to whitelist the WordPress files and directories only.

  1. Open .htaccess for editing
  2. Paste these rules at the top of it
    # Block access to the root site
    RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
    # Whitelist directories and files needed by WordPress
    RewriteCond %{REQUEST_FILENAME} !/wp-.* [NC]
    RewriteCond %{REQUEST_URI} !/wp-.* [NC]
    RewriteCond %{THE_REQUEST} !/wp-.* [NC]
    # Set like-to-like redirect URL for anything not whitelisted
    RewriteRule (.*) http://subdomain\.example\.com/$1 [R=301,L]
  3. Change the domain names in the rewrite rules
  4. Save the file.

Remember to remove $1 from the end of the final rule if you want all traffic to go to the homepage and not to be redirected to matching pages or posts.

Questions and Comments

If you like this guide or if you have questions about it, please use the comment form to let us know.

Sharing is caring!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of