Rails / Rack: getting query parameters from canonical_host middleware

I am using Rack Canonical Host middleware ( https://github.com/tylerhunt/rack-canonical-host ) with Rails to force www for all root requests (example.com becomes www.example.com). However, if a visitor is trying to access a valid subdomain of our application, we obviously don't want to force www. Here's an example using middleware:

Rails.application.config.middleware.use Rack::CanonicalHost do
  # the following return value will be used to set the canonical host
  'www.example.com'
end

      

As you can see, this is somewhat static, which is a problem. However, if I had access to the request parameters (e.g. subdomain, domain, etc.), I could check them and redirect accordingly (and only if needed).

Anyone have pointers?

+3


source to share


1 answer


Specify a parameter for this block



Rails.application.config.middleware.use Rack::CanonicalHost do |params|
  puts "PATH_INFO #{params['PATH_INFO']}"
end

      

+3


source







All Articles