First-Level Subdomains For All Sites in a WordPress Network

When you create a WordPress network (multisite) in subdomain mode, WordPress recommends that you remove www from the site URL, so the network (site #1) is served from the zone apex, and other sites are served from first-level subdomains. For example:

  • example.com
  • sub1.example.com
  • sub2.example.com

This is problematic if you need to use CNAMEs for your site (since you can’t CNAME a zone apex) or are migrating a site at www.example.com and don’t want to use redirects. If you ignore the recommendation, you’ll end up with second-level subdomains:

  • www.example.com
  • sub1.www.example.com
  • sub2.www.example.com

With some tinkering, it’s possible to have all sites use first-level subdomains, e.g.

  • www.example.com
  • sub1.example.com
  • sub2.example.com

Step 1: after creating the network with example.com as the URL, edit wp-config.php:

// Old: define( 'DOMAIN_CURRENT_SITE', 'example.com' );
define( 'DOMAIN_CURRENT_SITE', 'www.example.com' );
// Add this line so login cookies are accessible to all subdomains
define( 'COOKIE_DOMAIN', 'example.com' );

Step 2: take a database backup
Step 3: run the following queries on your WordPress DB (tested with 3.6.1 and 3.7-beta1):

UPDATE `wp_blogs` SET `domain` = 'www.example.com' WHERE `blog_id` = '1' AND `site_id` = '1';
UPDATE `wp_site` SET `domain` = 'www.example.com' WHERE `id` = '1';
UPDATE `wp_sitemeta` SET `meta_value` = 'http://www.example.com/' WHERE `site_id` = '1' AND `meta_key` = 'siteurl';
UPDATE `wp_options` SET `option_value` = 'http://www.example.com' WHERE `option_name` = 'home';
UPDATE `wp_options` SET `option_value` = 'http://www.example.com' WHERE `option_name` = 'siteurl';

Step 4 (optional): point the zone apex of example.com to 174.129.25.170 (wwwizer), which will redirect visitors from example.com to www.example.com.

Note: WordPress treats the www subdomain as a special case. If you use a different subdomain for the network, you’ll need to change each site after creating it:

  1. My Sites > Network Admin > Sites > Edit
  2. Remove second-level subdomain
  3. Leave “Update siteurl and home as well.” checked
  4. Save Changes

Leave a Reply

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