Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a URL, how can I get just the domain?

Given URLs like:

http://online.wsj.com/
http://online.wsj.com/article/SB10001424052970204409004577158764211274708.html
http://www.techcrunch.com/2012/01/13/techcrunch-coo/

Using Ruby/Rails, how can I return back just the domain?

online.wsj.com
online.wsj.com
techcrunch.com

No protocol, no slashes, just the subdomain if it's not www, and the domain, and ext?

like image 788
AnApprentice Avatar asked Jan 13 '12 18:01

AnApprentice


People also ask

How do I extract a domain name?

Any name registered in the DNS is a domain name. To extract a domain name from a URL, first remove the protocol (http://, https://, ftp://, etc.), then remove any subdomains (www., blog., etc.), then remove the top-level domain (.com, . net, . org, etc.).

How do I extract a domain from a URL in Google Sheets?

The =REGEXREPLACE() function is built-in Google Sheets and it extracts domains from URLs. What's great about is it's only a simple line of code that you can paste into your cell. The function is not super technical and you can change it any way you see fit.

How do I find the domain of a string?

First let's create a string with our URL (Note: If the URL isn't correctly structured you'll get an error). const url = 'https://www.michaelburrows.xyz/blog?search=hello&world'; Next we create a URL object using the new URL() constructor. let domain = (new URL(url));


1 Answers

Use Addressable::URI.parse and the #host instance method:

Addressable::URI.parse("http://techcrunch.com/foo/bar").host #=> "techcrunch.com" 
like image 72
Jim Deville Avatar answered Sep 18 '22 15:09

Jim Deville