New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request.subdomain not populating if host is localhost #12438

maheshguruswamy opened this Issue Oct 4, 2013 · 3 comments


None yet
3 participants

maheshguruswamy commented Oct 4, 2013

I have been trying to route to different controllers for different subdomains, without any luck. This is what I had

Petworkslabs::Application.routes.draw do
  get '/', to: 'custom#show', constraints: {subdomain: '/.+/'}, as: 'custom_root'
  get '/',  to: "welcome#home", as: 'default_root'

But for some reason the request getting routed to welcome#home. I stepped through the debugger and found that request.subdomain was being populated as nil, even though request.domain was populated as 'abc.localhost'. I suspect this subdomain is not getting populated because I am doing this on localhost, but it will be nice to have this work on localhost for development purposes. I ended by creating a custom matches? for the Subdomain class which looks something like this

class Subdomain
  def self.matches?(request)
    request.domain.split('.').size>1 && request.subdomain != "www"

This comment has been minimized.

ataber commented Oct 8, 2013

Out of curiousity, are you using Pow as your server or rails server?


This comment has been minimized.


pixeltrix commented Oct 8, 2013

If you look a the documentation for subdomain you can see that it takes an optional argument tld_length which defaults to 1. This means that when calculating the domain and subdomain it will assume that first component on the right is a top-level domain. If you pass 0 for tld_length then it will work as you want. Similarly, if you pass 2 it'll work for country domains like, e.g:

# tld_length = 1 (Default)
>> ActionDispatch::Http::URL.extract_subdomains('abc.localhost').first
=> nil
# tld_length = 0
>> ActionDispatch::Http::URL.extract_subdomains('abc.localhost', 0).first
=> "abc"
# tld_length = 2
>> ActionDispatch::Http::URL.extract_subdomains('', 2).first
=> "www"

You can configure the default tld_length in your config/environments/development.rb file, e.g:

MyApp::Application.configure do
  config.action_dispatch.tld_length = 0

Obviously, you can also configure it for test and production environments as well.

@pixeltrix pixeltrix closed this Oct 8, 2013


This comment has been minimized.

maheshguruswamy commented Oct 8, 2013

@pixeltrix Thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment