Get the subdomain from a URL
Publicsuffix.org seems the way to do. There are plenty of implementations out there to parse the contents of the publicsuffix data file file easily:
- Perl: Domain::PublicSuffix
- Java: http://sourceforge.net/projects/publicsuffix/
- PHP: php-domain-parser
- C# / .NET: https://github.com/danesparza/domainname-parser
- Python: http://pypi.python.org/pypi/publicsuffix
- Ruby: domainatrix, public_suffix
As Adam says, it's not easy, and currently the only practical way is to use a list.
Even then there are exceptions - for example in .uk
there are a handful of domains that are valid immediately at that level that aren't in .co.uk
, so those have to be added as exceptions.
This is currently how mainstream browsers do this - it's necessary to ensure that example.co.uk
can't set a Cookie for .co.uk
which would then be sent to any other website under .co.uk
.
The good news is that there's already a list available at http://publicsuffix.org/.
There's also some work in the IETF to create some sort of standard to allow TLDs to declare what their domain structure looks like. This is slightly complicated though by the likes of .uk.com
, which is operated as if it were a public suffix, but isn't sold by the .com
registry.
Anyone have any great ideas besides storing a list of all TLDs?
No, because each TLD differs on what counts as a subdomain, second level domain, etc.
Keep in mind that there are top level domains, second level domains, and subdomains. Technically speaking, everything except the TLD is a subdomain.
In the domain.com.uk example, "domain" is a subdomain, "com" is a second level domain, and "uk" is the TLD.
So the question remains more complex than at first blush, and it depends on how each TLD is managed. You'll need a database of all the TLDs that include their particular partitioning, and what counts as a second level domain and a subdomain. There aren't too many TLDs, though, so the list is reasonably manageable, but collecting all that information isn't trivial. There may already be such a list available.
Looks like http://publicsuffix.org/ is one such list—all the common suffixes (.com, .co.uk, etc) in a list suitable for searching. It still won't be easy to parse it, but at least you don't have to maintain the list.
A "public suffix" is one under which Internet users can directly register names. Some examples of public suffixes are ".com", ".co.uk" and "pvt.k12.wy.us". The Public Suffix List is a list of all known public suffixes.
The Public Suffix List is an initiative of the Mozilla Foundation. It is available for use in any software, but was originally created to meet the needs of browser manufacturers. It allows browsers to, for example:
- Avoid privacy-damaging "supercookies" being set for high-level domain name suffixes
- Highlight the most important part of a domain name in the user interface
- Accurately sort history entries by site
Looking through the list, you can see it's not a trivial problem. I think a list is the only correct way to accomplish this...