Track and display subdomains under their root domain
Adds sites.root_domain (indexed string, backfilled for existing rows)
computed via a naive "last two labels" heuristic (App\Support\RootDomain)
— good enough for example.com/somesite.es, but wrong for two-part
public suffixes like .co.uk. Doing that correctly needs the Mozilla
Public Suffix List; flagged as a known simplification rather than
built now.
Deliberately not a hard FK: like Backlink.target_domain, a subdomain
can get indexed before its root domain does (or vice versa), so
Site::subdomains()/rootSite() match on the domain string rather than
requiring the relationship to exist at write time.
Site detail page: a root domain shows a paginated "Subdomains"
section (same guest-preview cap as Pages/Backlinks); a subdomain
shows "Subdomain of {root}" linking back, when the root is itself
indexed.
Verified against real data: 21k.ro correctly lists its two known
subdomains (buzau.21k.ro, timisoara.21k.ro), each of which correctly
links back to 21k.ro. Added SubdomainsTest (root lists subdomains,
subdomain links to root, empty state) — full suite (31 tests) passes.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
/**
|
||||
* Naive "last two labels" root-domain heuristic — good enough for the common case
|
||||
* (example.com, somesite.es) but wrong for real-world two-part public suffixes like
|
||||
* .co.uk or .com.au, where it would treat "co.uk" as the root instead of
|
||||
* "example.co.uk". Doing that correctly means integrating the Mozilla Public Suffix
|
||||
* List; this is a deliberate simplification until that's worth the added complexity.
|
||||
*/
|
||||
class RootDomain
|
||||
{
|
||||
public static function of(string $domain): string
|
||||
{
|
||||
$labels = explode('.', strtolower(trim($domain, '.')));
|
||||
|
||||
if (count($labels) <= 2) {
|
||||
return implode('.', $labels);
|
||||
}
|
||||
|
||||
return implode('.', array_slice($labels, -2));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user