Read more

Ruby: Extract the hostname from a URL

Henning Koch
October 25, 2012Software engineer at makandra GmbH
url = 'http://www.foocorp.com/foo/bar'
URI.parse(url).host
# => www.foocorp.com

Note that this will raise an error if the given argument is not a URL.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

If you need the host's full URL without path, query, fragment etc., use URI.join Show archive.org snapshot with a clever twist:

url = 'http://www.foocorp.com:33546/foo/bar?query=foobar#hash'
URI.join url, '/'
# => http://www.foocorp.com:33546/
Posted by Henning Koch to makandra dev (2012-10-25 13:59)