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 the url.
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 the url.
Rails 3.0 has this capability built-in, you can access the subdomain from request.subdomain
.
You can also route based on the subdomain:
class SupportSubdomain
def self.matches?(request)
request.subdomain == "support"
end
end
Basecamp::Application.routes do
constraints(SupportSubdomain) do
match "/foo/bar", :to => "foo#bar"
end
end
If you're using 2.3, you'll need to use a plugin such as subdomain-fu.
Use the following method inside your controller
request.subdomains
This Returns an array of subdomains
request.subdomain
does not return an array. request.subdomains
does. Note the plural form of subdomain. –
Kenward 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://mcmap.net/q/470872/-how-to-parse-a-url-and-extract-the-required-substring
request.referrer
. Don't listen to @corvuszero. The question wasn't specific. Thanks! –
Upstanding mysite.co.uk
for example. –
Piglet account_location is also a good plugin. After using it, you can find the account based on different subdomains. And you can find out subdomain from url just by writing
request.subdomains(0).first
in your code.
Simple in your controller just do the following
unless request.subdomains.any?
#No domains available redirect
redirect_to subdomain: 'www'
end
You can use the SubdomainFu plugin. This plugin gives you a method current_subdomain which returns the current_subdomain of your app.
You can also have a look at this Railscast
UPDATE
You can also use request.subdomains
this will give you an array of subdomains.
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
current domain with subdomains:
"#{request.subdomain}.#{request.domain}"
# or
"#{request.subdomains.join(".")}.#{request.domain}"
.
if there's no subdomain; a more robust approach (if subdomain may be blank) is something like current_domain = (request.subdomains + [request.domain]).join('.')
–
Tarantula A bit late to the party but here's what I used in older versions of rails.
subdomain = request.subdomains.join('.')
It should be backwards compatible in newer versions
Recently worked on similar task - but the goal were to remove the subdomain from URL, ignoring the www
and any combination of .co.uk
or other top level domain. Maybe someone will find it useful:
def self.remove_subdomain_from_url(url)
root_domain = 'example'
uri = URI.parse(url)
host = uri.host.sub(/^www\./, '')
root_domain_index = host.split('.').find_index { |host_element| host_element.include?(root_domain) }
return url if root_domain_index.zero?
new_host = []
new_host.push('www') if url.include?('www')
new_host.concat(host.split('.')[root_domain_index..-1])
uri.host = new_host.join('.')
uri.to_s
end
And a bunch of tests that might help you to understand the code:
it 'does not remove www from url' do
expect(remove_subdomain_from_url('https://www.pl.example.com/path/?originalSubdomain=pl')).to eq(
"https://www.example.com/path/?originalSubdomain=de"
)
end
it 'removes subdomain from url' do
expect(remove_subdomain_from_url('https://www.de.example.com/path/?originalSubdomain=de')).to eq(
"https://www.example.com/path/?originalSubdomain=de"
)
end
it 'works properly with http' do
expect(remove_subdomain_from_url('http://de.example.com/path/?originalSubdomain=de')).to eq(
"http://example.com/path/?originalSubdomain=de"
)
end
it 'works properly with nested subdomains' do
expect(remove_subdomain_from_url('https://nested.subdomain.example.com/')).to eq('https://example.com/')
end
it 'works properly with nested top level domain(suffix)' do
expect(remove_subdomain_from_url('https://www.de.example.co.uk/path/?originalSubdomain=de')).to eq(
"https://www.example.co.uk/path/?originalSubdomain=de"
)
end
For those seeking a generic but robust solution, Mozilla started a Public Suffix List that is free to use and which is the best solution for generically parsing a hostname into its parts (subdomains, private domain, tlds). This resource was originally intended for browser manufacturers so that they can properly scope cookies and such (else you could create a SameSite cookie for co.uk
- and then any server registered under that public suffix could read / write to it).
Unfortunately the Public Suffix List is presently not published in an readily useful format; you will need to use an existing library or write your own parser to transform it into a Suffix Tree or what not so that you can use it to match the top-most domains in a hostname to a public suffix.
The list can be downloaded from: https://publicsuffix.org/list/public_suffix_list.dat
The format of the list is described in detail here: https://github.com/publicsuffix/list/wiki/Format#format
© 2022 - 2024 — McMap. All rights reserved.