How to get the subdomain value from a url?

后端 未结 9 2259
太阳男子
太阳男子 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 03:06

    Simple in your controller just do the following

    unless request.subdomains.any?
       #No domains available redirect
       redirect_to subdomain: 'www'
    end
    
    0 讨论(0)
  • 2020-12-09 03:13

    For anyone looking to get the subdomains on localhost using WEBrick:

    Put config.action_dispatch.tld_length = 0 into config/environments/development.rb and everything should work.

    Link to SO post here: Can I make Rails / WEBrick recognize entries in /etc/hosts as subdomains (instead of domains)?

    Link to Github post: https://github.com/rails/rails/issues/12438

    0 讨论(0)
  • 2020-12-09 03:14

    current domain with subdomains:

    "#{request.subdomain}.#{request.domain}"
    # or
    "#{request.subdomains.join(".")}.#{request.domain}"
    
    0 讨论(0)
提交回复
热议问题