Below is some code that will give just the SLD plus gTLD or ccTLD extension (note the exception below). I do not care about DNS.
The theory is as follows:
- Anything under 3 tokens stays as is e.g. "localhost", "domain.com", otherwise: The last token must be a gTLD or ccTLD extension.
- The penultimate token is considered part of the extension if it's length < 3 OR if included in a list of exceptions.
- Finally the token before that one is considered the SLD. Anything before that is considered a subdomain or a host qualifier, e.g. Www.
As for the code, short & sweet:
private static string GetDomainName(string url)
{
string domain = new Uri(url).DnsSafeHost.ToLower();
var tokens = domain.Split('.');
if (tokens.Length > 2)
{
//Add only second level exceptions to the < 3 rule here
string[] exceptions = { "info", "firm", "name", "com", "biz", "gen", "ltd", "web", "net", "pro", "org" };
var validTokens = 2 + ((tokens[tokens.Length - 2].Length < 3 || exceptions.Contains(tokens[tokens.Length - 2])) ? 1 : 0);
domain = string.Join(".", tokens, tokens.Length - validTokens, validTokens);
}
return domain;
}
The obvious exception is that this will not deal with 2-letter domain names. So if you're lucky enough to own ab.com you'll need to adapt the code slightly. For us mere mortals this code will cover just about every gTLD and ccTLD, minus a few very exotic ones.