Get domain without subdomain from URL

What is the correct way to get a domain from a URL without subdomains?

In Java, from a string, you can create a new URL (urlString) and call getHost () on the URL, but you have subdomains with it.

The problem is that there may be hosts such as: subhost.example.com as well as subhost.example.co.uk

There are several other of these two parts, such as co.uk (see the list https://wiki.mozilla.org/TLD_List ).

It seems to me that the only right way to get only a domain is to search the TLD list, remove the TLD from the end of the host and take everything to the last period in the host. Is there an existing method that does this? I did not see it in java.net.URL, and I beat apache a bit, but could not find it.

+14
java subdomain dns
Jul 08 '10 at 0:20
source share
2 answers

I know this is a few years later, but if someone stumbles on this, try the following:

InternetDomainName.from("subhost.example.co.uk").topPrivateDomain().name 

The above will return example.co.uk.

+16
Jun 25 '14 at 13:24
source share

Not sure if the above answer is correct:

 InternetDomainName.from("test.blogspot.com").topPrivateDomain() -> test.blogspot.com 

This works better in my case:

 InternetDomainName.from("test.blogspot.com").topDomainUnderRegistrySuffix() -> blogspot.com 
0
May 17 '19 at 10:54
source share



All Articles