Problem

When Domain Alias resolves a request via hook_domain_request_alter(), the domain stored in the negotiation context retains its canonical hostname instead of the alias hostname. For example, if the request comes in on staging.example.com (an alias for example.com), the active domain's hostname is example.com.

This works today only by accident: every code path that needs a hostname reloads the domain from storage, which triggers hook_domain_load, which rewrites the hostname. Comparisons use entity IDs, never hostnames. But any code calling $negotiator->getActiveDomain()->getHostname() directly gets the wrong value.

Steps to reproduce

  1. Create a domain example.com
  2. Create a non-default alias staging.example.com pointing to it
  3. Visit staging.example.com
  4. In a hook or custom code, call \Drupal::service('domain.negotiator')->getActiveDomain()->getHostname()
  5. Result: example.com (canonical) instead of staging.example.com (alias)

Root cause

In DomainResolver::resolveDomain(), after hook_domain_request_alter fires, the domain is returned with the canonical hostname. The alias hostname rewriting only happens later in hook_domain_load, which cannot run at negotiation time because the active domain is not yet set.

Proposed fix

Apply the alias hostname rewriting in hook_domain_request_alter itself, so the domain returned by resolveDomain() already has the correct hostname. This would make the active domain in the context immediately correct and remove the dependency on hook_domain_load for the active domain's hostname.

Note

This is a pre-existing bug, not a regression. The same pattern existed in the old DomainNegotiator::setRequestDomain() before the DomainResolver refactoring.

Issue fork domain-3583723

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mably created an issue. See original summary.

mably’s picture

Status: Active » Needs review

  • mably committed aa18b48c on 3.x
    fix: #3583723 Active domain has canonical hostname instead of alias...
mably’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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