How to get the subdomain value from a url?

后端 未结 9 2263
太阳男子
太阳男子 2020-12-09 02:15

how can I get the subdomain value in rails, is there a built-in way to do this?

e.g.

test123.example.com

I want the test123 part of

9条回答
  •  遥遥无期
    2020-12-09 02:52

    In case you are working with a string, and assuming it can be a true URI, you can do this to extract the subdomain.

    require 'uri'
    
    uri = URI.parse('http://test123.example.com')
    uri.host.split('.').first
    => "test123"
    

    https://stackoverflow.com/a/13243810/3407381

提交回复
热议问题