Reputation: 108
I have a Ruby on Rails app hosted on Heroku. As part of the app, I need to take a host name that has been entered and determine the base domain so we can do an automated NS lookup to find the host name's authoritative name server using Resolv.
Given the following examples entered into the app:
www.google.com
online.domain.com
www.somedomain.co.uk
sub.host.domain.com
How do I get just the base domains:
google.com
domain.com
somedomain.co.uk
domain.com
The tricky part is we can't just take the last two parts of the domain name due to international URLs. But I don't know of any other way to get a host name stripped down to where we can find the NS record for it. Curious about any suggestions.
Upvotes: 4
Views: 2792
Reputation: 11235
Two options that I'm aware of:
Create a regex that matches all possible domain extensions, then exract the preceding hostname. You can find a public list for all possible domain extensions here: https://publicsuffix.org/list/.
Use the domainatrix gem (https://github.com/pauldix/domainatrix) which has a registry of all domain extensions collected from the above list:
url = Domainatrix.parse("http://www.example.co.uk") => # url.domain => "example" url.public_suffix => "co.uk"
Upvotes: 2