Can WordPress and WordPress Multisite share the same domain?

Using Trellis, I would like to have a separated marketing website (WordPress) and an application (WordPress MultiSite) which share the same domain.

The marketing website would be located at:

https://site.com and https://www.site.com (redirects to root)

The application website would be located at:

https://app.site.com

That’s easy enough on its own, but an issue arrises when additional blogs are added to the WordPress Multisite. They all use the primary site’s sub domain app.site.com. This is expected, but I would like to change it…

From:

https://blog1.app.site.com
https://blog2.app.site.com
https://blog3.app.site.com

To:

https://blog1.site.com
https://blog2.site.com
https://blog3.site.com

Is this in any way possible?

Here are some thoughts to get you started. I didn’t test. This may not work.

The details below describe three steps:

  • edit three Trellis vars:
    • wp_home
    • wp_siteurl
    • multisite_subdomains_wildcards
  • edit COOKIE_DOMAIN in Bedrock’s config/application.json
  • change one URL setting in the WP admin

wordpress_sites

wordpress_sites:
  # marketing site
  site.com:
    site_hosts:
      - canonical: site.com
        redirects:
          - www.site.com
    multisite:
      enabled: false
    ...
  # app / blogs
  app.site.com:  # <-- site key must differ from site key above if on same server
    site_hosts:
      - canonical: app.site.com
    multisite:
      enabled: true
      subdomains: true
    env:
      wp_home: https://site.com
      wp_siteurl: https://site.com/wp
    ...

Regarding app.site.com, I’m guessing the wp_home value of site.com (no app subdomain) will mean that WordPress will default to creating new subdomain blogs at blog1.site.com etc. (I’m not certain).

Domain mapping for app.site.com

However, the wp_home above will probably mean that WordPress will interpret the primary site as being at site.com. I think you could set up domain mapping to change the URL to app.site.com for this site:

Go to Network Admin -> Sites -> Edit Site -> edit the site URL
(see toscho and discourse thread)

For WP admin logins to work, edit your Bedrock config/application.json:

- define('COOKIE_DOMAIN', '.' . env('DOMAIN_CURRENT_SITE'));
+ define('COOKIE_DOMAIN', '');

:warning: I don’t know if there are negative consequences of this COOKIE_DOMAIN change. I identified this change as necessary to avoid a login redirect loop like this and I didn’t look further.

Nginx conf server_name

The wordpress_sites above would normally create the following for Nginx server_name:

server_name app.site.com *.app.site.com;

We need to find a way to make this change:

- server_name app.site.com *.app.site.com
+ server_name app.site.com *.site.com

I normally wouldn’t encourage anyone to edit a helper variable like multisite_subdomains_wildcards, but it may be simpler than working out a child template.

- multisite_subdomains_wildcards: "{{ item.value.multisite.subdomains | default(false) | ternary( site_hosts_canonical | map('regex_replace', '^(www\\.)?(.*)$', '*.\\2') | list, [] ) }}"
+ multisite_subdomains_wildcards: "{{ item.value.multisite.subdomains | default(false) | ternary( site_hosts_canonical | map('regex_replace', '^(app\\.)?(.*)$', '*.\\2') | list, [] ) }}"

The relevant portion of the diff above is toward the end of the line:

- ...('regex_replace', '^(www\\.)...
+ ...('regex_replace', '^(app\\.)...

Normally multisite_subdomains_wildcards just ignores a www portion, e.g., it would make:

www.example.com *.example.com

In your case we’d like to ingore the app portion (from canonical way above in wordpress_sites, yielding:

app.site.com *.site.com

HTTPS

It looks like you intend to use https. Until Let’s Encrypt starts offering wildcard certs in January 2018, you’ll need to obtain a wildcard cert elsewhere (unless you know the exact blog subdomains and want to list them in site_hosts of wordpress_sites). I’m guessing you are already familiar with the related discourse threads on the topic.

1 Like