The current UX for installing a subdir site (example.com/site1) is confusing. To illustrate, here's the description for the "Domain name" field, when hosting_subdir.module is enabled:

Hint: To create a site using a subdirectory, name it using a subdomain (e.g., foo.example.com), and then enter an alias below for the desired subdirectory (e.g., example.com/foo). Important: subdomain (foo) and subdirectory name (foo) must be identical. If you plan to also use the root domain (e.g., example.com), you must create a site with the root domain *before* adding sites in its subdirectories. Note: Once the first site in a subdirectory is created and the parent site also exists, the parent site must be re-verified (just once) to turn on the web server configuration for the first (and any future) sites in its subdirectory. All sites sharing the same parent URL can use any profile, on any platform, and can be freely migrated, cloned, etc.

But wait! There's more (for "Domain Aliases"):

Note: If the site is using a subdirectory, there should be at least one special alias with a subdirectory path (e.g., example.com/foo) below. Important: subdomain (foo) and subdirectory name (foo) must be identical. Redirection to this alias will also work, if enabled. This is useful if you do not want the site to be accessible from its subdomain (e.g., foo.example.com). You can also add more standard aliases (e.g., bar.example.com, green.example.com).

Stripped down, this process is:

  1. Remove the subdirectory from your desired URL and prepend it as a sub-domain
  2. Add an alias for the desired URL
  3. Click "Add alias" button (so that the alias appears as a redirect target)
  4. Select the desired URL as a redirect target

Ideally the process should be identical to creating any other site. That is, just use the desired URL in the title, and let Aegir do its thing.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ergonlogic created an issue. See original summary.

ergonlogic’s picture

The main challenge, as I see it, it that site node titles are special, as they're used throughout the code as the site domain, to construct site paths, etc. I don't think it's feasible to change that behaviour without potentially introducing lots of bugs.

Instead, I propose that we hook into site form validation and automate the steps I outlined above. The only major drawback to this approach, that I see, is that the site would be magically renamed, which could cause more confusion. However, #3036884: Use redirect target for site title in UI should address that.

colan’s picture

This looks like a duplicate of #2186923: Allow example.com/foo activate subdirs on a site, but since there's more info here, I'll mark that one as a duplicate of this one.

memtkmcc’s picture

Very old stuff, I think Grace wrote a part of this complex how-to.

The key here is that we are using the special alias in the form of example.com/foo to tell Aegir that foo.example.com site should be actually a subdir site.

Perhaps we could move this logic around so example.com/foo as a "displayed" site name could trigger the subdir logic instead?

I said "displayed" in the sense that /foo would be ignored otherwise (filtered out) and only used as a flag to signal that the site should be a subdir site -- which would be actually the best from the UX point of view -- exactly what the user expects to see in the subdir site's name (also on the sites list) to find the subdir site.

We could then automate having subdomain matching the subdir without having to explain this in the form.

ergonlogic’s picture

#3036884: Use redirect target for site title in UI is now complete (pending review). So from a display stand-point, we're always using the subdir to display site titles, to construct task titles, in Views, etc.

I'm going to ignore, for the moment, the use case where a user wants to create a sub-subdir site (eg. example.com/subdir/site1), etc. I'm not sure it's supported at the moment anyway. That said, I'll try not to paint ourselves into a corner that'd make such stuff harder down the road.

So, to re-iterate the next steps:

  1. Detect slashes in domain URLs, which act to trigger the subsequent behaviours.
  2. Validate both the desired subdir URL and the corresponding sub-domain URL
  3. Re-write the domain URL (site node title) to the proper sub-domain URL
  4. Create an alias for the desired subdir URL
  5. Create a redirect to the desired subdir URL

We'll want to make sure that (1) and (2) happen on all instances of site forms (ie, add, clone, migrate, etc.)

(3), (4) and (5) should probably all happen in hook_node_insert() and/or hook_node_update(), so that we can trigger the same behaviour programmatically.

ergonlogic’s picture

We'll also want to check whether Let's Encrypt certificate generation is picking up the aliases and/or the redirection target.

ergonlogic’s picture

I added hooks to the allow additional automatic aliases (e.g., 'www') to be added: https://cgit.drupalcode.org/hosting/commit/?h=dev/consensus&id=0b417887f.... That should give us a convenient way to inject our subdir alias.

But, that gets called from an insert hook, so we need to keep the slash in the title throughout the validate hooks. Currently, we're validating that the domain is an FQDN, which currently excludes slashes. I think the best work-around here is to provide an alter hook for the URL, which'll allow us to pass validation, and call the same hook in hosting_site_nodeapi_site_presave().

That way, we should only need to set the redirection target in our hosting_subdirs_node_insert().

ergonlogic’s picture

Status: Active » Needs review

I added an additional hook to allow the site domain to be altered: https://cgit.drupalcode.org/hosting/commit/?h=dev/consensus&id=302ef4b80...

That allows us to do as suggested above: https://cgit.drupalcode.org/hosting/commit/?h=dev/consensus&id=a33527dcf...

This was somewhat tricky, since we have to set the module weight for hosting_subdirs to ensure that it runs before hosting_site. Specifically, we need hosting_subdirs_node_presave() to run before hosting_site_node_presave() in order to be able to add the proper alias and redirection.

This works for me during site installation. I haven't yet tested on migrate (aka. rename) or clone tasks. Also, inline docs will need some cleanup. Still, setting to "needs review" to get some feedback.

ergonlogic’s picture

Status: Needs review » Needs work

Migrate tasks now work, but I'm seeing Nginx hang when the task completes. I'll need to dig into this a bit more. It might just be insufficient memory on the dev VM.

Anyway, migration (and rename) tasks work with automatic subdir redirection, but clone tasks don't. They end up losing the subdir, I believe because of the import task that they spawn. Either way, the cloned site can be renamed, but it's bothersome.

I'm also seeing a bug in the node form validation that doesn't let me manually save nodes.

ergonlogic’s picture

Having bumped up the VM's RAM, migrate tasks complete without issues.

However, it appears that the old alias/redirection was persisting on the backend. Triggering a verify in the front-end after a migrate tasks is enough to get it working though. I added this in a post task hook, but I'm not satisfied with this solution. As it stands, the verify task doesn;t get triggered until the end of the migrate task. Any tasks queued in since the migrate will have to run before the verify that completes the migration.

ergonlogic’s picture

I'm also seeing a bug in the node form validation that doesn't let me manually save nodes.

This appears to be an unexpected behaviour of hook_form_alter(). Apparently it doesn't get run if the form is rebuilt following a validation failure. I moved the relevant changes into '#after_build' functions as per https://www.drupal.org/project/drupal/issues/671574#comment-3061880. This appears fixed now.

ergonlogic’s picture

Cloning subdir sites works initially, but the new site drops the subdir alias and redirection after the resulting 'import' task completes. This is presumably because the site doesn't have these on the backend, and so the 'import' task removes then from the front-end too.

This is pretty much the same thing that's happening with 'migrate' tasks. We fixed those by just running a 'verify' after the 'migrate', since the alias and redirection still exist on the front-end.

When the 'import' is triggered, the site has the proper alias and redirection. Perhaps we can access those values, and reset them after the 'import' completes, and trigger another verify. Really all we need is to set the title back to the original subdir value, and then our presave hook can do its thing.

[...] it appears that the old alias/redirection was persisting on the backend. Triggering a verify in the front-end after a migrate tasks is enough to get it working though [...] but I'm not satisfied with this solution [...]

It seems to me that the "proper" solution here is to pass the alias and redirection into the 'migrate' task, and thus forgo the extra 'verify'. This would presumably also fix the 'import' task, since the alias and redirection would exist on the back-end, and thus populate the front-end properly.

ergonlogic’s picture

Unfortunately, it doesn't look like it'd be easy to alter the data coming in from the back-end. So, we now cache our alias and redirection on the site node in hosting_subdirs_post_hosting_import_task(), and then re-instate them in hosting_subdirs_node_presave(). Not ideal, but it seems to work well.

I think all that's left is to simplify the inline docs.

I added #3040392: Ensure that subdirectory sites work properly with Let's Encrypt to track the only other pending task I could find in this issue.

ergonlogic’s picture

Status: Needs work » Needs review

This is ready for review, inline docs notwithstanding.

ergonlogic’s picture

I've pushed some changes to the inline docs. I hope it's clearer now.

  • llamech committed 9774aac on 3036890-simplify-subdirs authored by ergonlogic
    Issue #3036890: Simplify subdir site installation.
    
llamech’s picture

llamech’s picture

To confirm that the login link works for D8 sites on Nginx:

  1. Install a D8 subdir site on Nginx.
    1. Observe the simplified documentation.
    2. Create the site with a subdir URL (such as example.com/site1).
    3. Note that you don't have to do anything with aliases or redirects.
  2. Ensure install process produces correct URL.
    1. Note the site URL should be listed as example.com/site1
    2. Likewise, tasks, site listings and login links should all use the subdir URL.
    3. Observe that the proper alias and redirect have been created automatically.
  3. Ensure login link redirects to the correct URL.
    1. Ensure login link actually logs in.
    2. Ensure internal links work (on subdir site) when logged in.
    3. Log out.
    4. Ensure internal links work (on subdir site) when logged out.
  4. Ensure password reset process produces correct URL.
    1. Repeat steps from previous section ("Ensure login link... etc.").
  5. Clone the site using a new subdirectory URL (e.g. example.com/site2).
    1. Repeat steps above on cloned site, as desired.
  6. Migrate the site using a new subdirectory URL (e.g. example.com/site3).
    1. Repeat steps above on migrated site, as desired.
llamech’s picture

We've identified a hook_update() that is needed for this fix to work on preexisting Aegirs that are running subdir sites on nginx. Basically, we just need to re-verify the hostmaster servers, so that the nginx server vhosts are updated. Site vhosts will also need to be rewritten, but we don't want to do that in a hook_update(); instead we will update the release notes to this effect.

To test:

1. Reinstall Aegir from scratch on the 7.x-3.x branch.
2. Switch to the new branch (3036890-simplify-subdirs).
3. Run update.php
4. Repeat tests from the previous comment.

ergonlogic’s picture

I added the update hook in this commit.

  • colan committed 0394375 on 7.x-3.x
    Issue #3036890 by colan, ergonlogic, llamech: Merge branch '3036890-...
  • llamech committed 9774aac on 7.x-3.x authored by ergonlogic
    Issue #3036890: Simplify subdir site installation.
    
colan’s picture

Status: Needs review » Fixed

I successfully tested everything and merged the issue branches into 7.x-3.x.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

colan’s picture

As migrations don't seem to be maintaining the new UX right now, I've created a follow-up issue @ #3068257: Migrating a subdirectory site reverts to internal domain & loses alias.

colan’s picture