Skip to content

Commit

Permalink
Add support for domain aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
fago committed Jul 25, 2019
1 parent b35163e commit 69bf5e2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The following environment variables may be set to configure the request matcher:
| APP_MULTISITE_DOMAIN | ~ | stage.codebase.dev | A common base domain for all sites. Required when multisite base domains should be used. |
| APP_MULTISITE_DOMAIN_PREFIX_SEPARATOR | No | _ | The separator between the site name and the common multisite base domain. Defaults to '_'. |
| APP_SITE_DOMAIN__{{ SITE }} | ~ | site-a.com | The per-site domain - required when per-site domains should be used. One variable per site must be provided with dashes replaced to underscores, e.g. for site-a the variable name would be `APP_SITE_DOMAIN__site_a` |
| APP_SITE_DOMAIN_ALIASES__{{ SITE }} | ~ | site-a.hoster.com,site-a.hoster.com | Comma separated, per-site domain aliases that are allowed in addition to the main domain. Useful when access should be allowed via some non-primary domains also; e.g., when behind a CDN. One variable per site must be provided with dashes replaced to underscores, e.g. for site-a the variable name would be `APP_SITE_DOMAIN_ALIASES__site_a` |

## Results

Expand Down
10 changes: 8 additions & 2 deletions src/RequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,21 @@ public function match(Request $request = NULL) {
if (empty($site_domain)) {
throw new RequestMatchException("Missing API_SITE_DOMAIN environment variable for site " . strip_tags($current_site) . ".");
}
// Support additional aliases.
$site_domains[] = $site_domain;
if ($aliases = getenv('APP_SITE_DOMAIN_ALIASES__' . str_replace('-', '_', $current_site))) {
$site_domains = array_merge($site_domains, array_map('trim', explode(',', $aliases)));
}

if (preg_match('/^' . str_replace('.', '\.', $site_domain) . '$/', $host)) {
$domains_regex = '(' . str_replace('.', '\.', implode('|', $site_domains)) . ')';
if (preg_match('/^' . $domains_regex . '$/', $host)) {
$site_main_host = $site_domain;
$site = $current_site;
$site_variant = NULL;
break;
}
// Check for a site-variant match.
if (preg_match('/^' . str_replace('.', '\.', '(' . $variants . ')' . $this->variantSeparator . $site_domain . '$') . '/', $host, $matches)) {
if (preg_match('/^' . str_replace('.', '\.', '(' . $variants . ')' . $this->variantSeparator) . $domains_regex . '$/', $host, $matches)) {
$site_main_host = $site_domain;
$site = $current_site;
$site_variant = $matches[1];
Expand Down
45 changes: 45 additions & 0 deletions tests/src/RequestMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,51 @@ public function testMatch() {
$this->assertHostDoesNotMatch('admin.site-b.com');
$this->assertHostDoesNotMatch('api.site-b.com');
$this->assertHostDoesNotMatch('api--site-b.com');

// Test with per-site domains and aliases.
putenv("APP_MULTISITE_DOMAIN");
putenv("APP_SITE_VARIANT_SEPARATOR=.");
putenv("APP_SITE_VARIANTS=api admin");
putenv("APP_DEFAULT_SITE=site-a");
putenv("APP_SITES=site-a site-b");
putenv("APP_SITE_DOMAIN__site_a=site-a.com");
putenv("APP_SITE_DOMAIN_ALIASES__site_a=site-a.alias.com,site-a.alias2.com");
putenv("APP_SITE_DOMAIN__site_b=site-b.com");
$this->assertHostMatches('site-a.com', 'site-a', '');
$this->assertHostMatches('api.site-a.com', 'site-a', 'api');
$this->assertHostMatches('admin.site-a.com', 'site-a', 'admin');
$this->assertHostMatches('site-a.alias.com', 'site-a', '');
$this->assertHostMatches('api.site-a.alias.com', 'site-a', 'api');
$this->assertHostMatches('admin.site-a.alias.com', 'site-a', 'admin');
$this->assertHostMatches('site-a.alias2.com', 'site-a', '');
$this->assertHostMatches('api.site-a.alias2.com', 'site-a', 'api');
$this->assertHostMatches('admin.site-a.alias2.com', 'site-a', 'admin');
$this->assertHostDoesNotMatch('site-a.alias3.com', 'site-a', '');
$this->assertHostDoesNotMatch('api.site-a.alias3.com', 'site-a', 'api');
$this->assertHostDoesNotMatch('admin.site-a.alias3.com', 'site-a', 'admin');
$this->assertHostMatches('api.site-b.com', 'site-b', 'api');
$this->assertHostMatches('admin.site-b.com', 'site-b', 'admin');
$this->assertHostMatches('site-b.com', 'site-b', '');
$this->assertHostDoesNotMatch('com');
$this->assertHostDoesNotMatch('foo.site-b.com');
$this->assertHostDoesNotMatch('api--site-b.com');

putenv("APP_MULTISITE_DOMAIN");
putenv("APP_SITE_VARIANTS");
putenv("APP_SITE_VARIANT_SEPARATOR=.");
putenv("APP_DEFAULT_SITE=site-a");
putenv("APP_SITES=site-a site-b");
putenv("APP_SITE_DOMAIN__site_a=site-a.com");
putenv("APP_SITE_DOMAIN_ALIASES__site_a=site-a.alias.com");
putenv("APP_SITE_DOMAIN__site_b=site-b.com");
$this->assertHostMatches('site-a.com', 'site-a', '');
$this->assertHostMatches('site-a.alias.com', 'site-a', '');
$this->assertHostMatches('site-b.com', 'site-b', '');
$this->assertHostDoesNotMatch('com');
$this->assertHostDoesNotMatch('foo.site-b.com');
$this->assertHostDoesNotMatch('admin.site-b.com');
$this->assertHostDoesNotMatch('api.site-b.com');
$this->assertHostDoesNotMatch('api--site-b.com');
}

/**
Expand Down

0 comments on commit 69bf5e2

Please sign in to comment.