Alex Rivera | Logout

What's the best way to parse URLs to extract the domain?

Asked 2011-02-27T12:52:53.820
11

Possible Duplicate:
Ruby code to extract host from URL string

I found this module called URI which can parse the url. (I'm pretty new to ruby. Is 'module' synonymous with 'library' in this case?) You can then extract the host name.

uri = URI.parse("http://www.ruby-lang.org/")
...
p uri.host
# => "www.ruby-lang.org"

From this, I suppose you could remove 'www.' and keep other subdomains using regular expressions.

Does anyone have a more straight-forward alternative or does this approach seem right?

Edit
Report

1 Answer

11

So while posting my own answer, I'm not saying that gems like domainatrix or public_suffix_server aren't good elegant solutions (although the latter bugged on me immediately which caused me to go this route).

People suggesting using split() made me realize that I could just sub out 'www.' if it existed and otherwise leave domains as they are without installing gems and using 1 simple line of code:

url = request.original_url
domain = URI.parse(url).host.sub(/\Awww\./, '')

This works with subdomains and multi-part suffixes (eg. co.uk). Anybody see anything wrong with this?

EDIT: Thanks sorens for pointing out the weak regex I was originally using. This expression is certainly better.

answered 2011-02-28T00:27:26.727

Your Answer