From: Justin Collins Date: 2009-06-23T17:11:05+09:00 Subject: Re: how to extract domain name without sub domain from url Chem Leakhina wrote: > Hi everyone, > > Does anyone know how to extract domain name without sub domain from url? > > Example: http://test.domain.com => http://domain.com > > Please give me an example code in ruby. > > Thanks, > Leakhina > This is actually quite difficult, because there is a multitude of possible second-level domains which can be used (such as .co.uk), and they are not really standardized. Just picking one at random, the country of Jordan has .com.jo, .net.jo, .gov.jo, .edu.jo, .org.jo, .mil.jo, .name.jo, and .sch.jo. If one were to ignore such things, then it becomes easier: $ irb irb(main):001:0> require 'uri' => true irb(main):002:0> u = URI.parse "http://test.domain.com/" => # irb(main):003:0> u.host => "test.domain.com" irb(main):004:0> u.host.split(".")[-2,2] => ["domain", "com"] irb(main):005:0> u.host.split(".")[-2,2].join(".") => "domain.com" However, as mentioned above, there are a lot of domains this will not work for. -Justin