imderek
imderek

Reputation: 1316

Rails/Rack: retrieving request params from within canonical_host middleware

I'm using the Rack Canonical Host middleware (https://github.com/tylerhunt/rack-canonical-host) with Rails to force the www for all root requests (example.com becomes www.example.com). However, if a visitor is attempting to access a valid subdomain of our app, we obviously don't want to force www. Here's the example usage of the 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 it's somewhat static, which is a problem. However, if I had access to the request parameters (eg subdomain, domain, etc) I could check against them, and redirect accordingly (and only if need be).

Anyone have any pointers?

Upvotes: 3

Views: 1014

Answers (1)

gmoore
gmoore

Reputation: 5566

Specify a parameter for this block

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

Upvotes: 4

Related Questions