# Needed only the Domain name from an url

**URL:** <https://www.ruby-forum.com/t/needed-only-the-domain-name-from-an-url/166164>\
**Category:** Ruby\
**Created:** [April 22, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/needed-only-the-domain-name-from-an-url/166164 "2009-04-22T14:28:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [April 22, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/needed-only-the-domain-name-from-an-url/166164/1 "2009-04-22T14:28:13Z")

</div>

hi,  
Suppose if i give,  
url=“[www.google.com/picasa/images/1](http://www.google.com/picasa/images/1)”

I need only the string “google” which is the domain name.

right now i have tried this,

## require ‘rubygems’ require ‘uri’ url = “[http://www.google.com/picasa/images/1](http://www.google.com/picasa/images/1)” puts url = URI.parse(url).host

## output: “[google.com](http://google.com)”

but i want only the word “google”. please help.

Pts:  
the domain name can also be, “google.co.in”  
ie) for example, “[http://www.google.co.in/picasa/images/1](http://www.google.co.in/picasa/images/1)”

Thanks in advance,  
srikanth

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [April 22, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/needed-only-the-domain-name-from-an-url/166164/2 "2009-04-22T15:26:39Z")

</div>

On Wed, Apr 22, 2009 at 2:28 PM, Srikanth J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> url = “[http://www.google.com/picasa/images/1](http://www.google.com/picasa/images/1)”  
> Thanks in advance,  
> srikanth

Technically [google.com](http://google.com) is the domain  
If I run your code, I get “[www.google.com](http://www.google.com)” as the output, not  
“[google.com](http://google.com)”  
This poses an interesting problem because a domain can be:  
[one.two.three.four.google.co.uk](http://one.two.three.four.google.co.uk)  
What is the domain for you in that?

Otherwise you could do something like  
uri.host.sub(/.\*?.?([^.]+)(?:.\w{3}|.\w{2,3}.\w{2,3})$/, ‘\1’)

It may not be the prettiest regex but it gets the job done for the  
following:

- [one.two.google.com](http://one.two.google.com)
- one.two.google.co.in
- www.somesite.com.au
- [www.one.co.uk](http://www.one.co.uk)  
but it won’t work for
- [www.one.com](http://www.one.com) (but it will work for [one.com](http://one.com))

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [April 25, 2009, 9:23am UTC](https://www.ruby-forum.com/t/needed-only-the-domain-name-from-an-url/166164/3 "2009-04-25T09:23:01Z")

</div>

If you are using a rails app then something similar to this can be done:

In the application controller  
private

```
           def locate_subdomain

           end

```

On Fri, Apr 24, 2009 at 6:01 PM, Raveendran P. \<

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [April 25, 2009, 9:26am UTC](https://www.ruby-forum.com/t/needed-only-the-domain-name-from-an-url/166164/4 "2009-04-25T09:26:32Z")

</div>

On Sat, Apr 25, 2009 at 12:44 PM, kranthi reddy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> If you are using a rails app then something similar to this can be done:
> 
> In the application controller  
> private
> 
> ```
> def locate_subdomain
> domain = request.domain # for just the domain
> 
> ```
> 
> name

```
                          subdomain = request.subdomain # for the

```

sudomain

---

<div class="post-metadata">

**Author:** ![srikanth\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@srikanth\_j](https://www.ruby-forum.com/u/srikanth_j)\
**Post date:** [April 24, 2009, 2:31pm UTC](https://www.ruby-forum.com/t/needed-only-the-domain-name-from-an-url/166164/5 "2009-04-24T14:31:49Z")

</div>

Srikanth J. wrote:

> hi,  
> Suppose if i give,  
> url=“[www.google.com/picasa/images/1](http://www.google.com/picasa/images/1)”
> 
> I need only the string “google” which is the domain name.
> 
> right now i have tried this,
> 
> ## require ‘rubygems’ require ‘uri’ url = “[http://www.google.com/picasa/images/1](http://www.google.com/picasa/images/1)” puts url = URI.parse(url).host
> 
> ## output: “[google.com](http://google.com)”
> 
> but i want only the word “google”. please help.
> 
> Pts:  
> the domain name can also be, “google.co.in”  
> ie) for example, “[http://www.google.co.in/picasa/images/1](http://www.google.co.in/picasa/images/1)”
> 
> Thanks in advance,  
> srikanth

Hi Sri,

Are you got the solution ?

If Yes, then update here about it…

Regards  
P.Raveendran

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...
