Parsing URLs

I wanted to parse a URL and remove any ‘www’ type prefixes and just get the raw domain name.

RUBY

require 'uri'
 
def domain_name(url)
  #remove the http:// with URI.parse
  base_url = URI.parse(url).host
 
  #base_url.split returns an array, ex. ["www", "cnet", "com"]
  url_array = base_url.split('.')
 
  #url may be www.example.com or example.com
  if url_array.count == 3
    url_array.shift
    url_array.join('.')
  elsif url_array.count == 2
    base_url
  end 
end
 
p domain_name('http://www.cnet.com')    #=> "cnet.com"
p domain_name('http://httpbin.org')     #=> "httpbin.org"
p domain_name('http://mail.google.com') #=> "google.com"

JAVASCRIPT

function domainName(str){
  var baseURL = str.split("http://")[1];
  var urlArray = baseURL.split('.');
 
  if (urlArray.length == 3){
    urlArray.shift();
    return urlArray.join('.');
  }
  else{
    return baseURL;
  }
}
 
domainName("http://www.cnet.com");    // "cnet.com"
domainName("http://httpbin.org");     // "httpbin.org"
domainName("http://mail.google.com"); // "google.com"

Leave a Reply