My Host

19 views
Skip to first unread message

John Hinnegan

unread,
Feb 10, 2012, 1:00:54 PM2/10/12
to rubyonra...@googlegroups.com
I need to get some absolute redirects working -- basically I set callback hooks to external services, but I want these hooks to use my configured CNAME's.  I'm trying to find a way so that my app doesn't have to know about these CNAME's.  So I'm looking at the request in the controller to try and get the base URL where I'm running.

To test, I'm using localtunnel.  I have found what I'm looking for in 3 places, and I'm wondering if one is better than another or if one is more reliably set than another.

request.env["SERVER_NAME"]
request.host
request.env["HTTP_HOST"]

All three of these have the value I'm looking for (e.g. "3eym.localtunnel.com") .  Is there any difference between them?  Should I favor one over another?

Philip Hallstrom

unread,
Feb 10, 2012, 2:17:54 PM2/10/12
to rubyonra...@googlegroups.com

I'd use request.host...

That calls raw_host_with_port (and trims the port). raw_host_with_port looks like this:

def raw_host_with_port
if forwarded = env["HTTP_X_FORWARDED_HOST"]
forwarded.split(/,\s?/).last
else
env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
end
end

So does some extra checking for you...

https://github.com/rails/rails/blob/5f67cfeda116f2b932ef72781420aa37e62437ca/actionpack/lib/action_dispatch/http/url.rb#L92

-philip

John Hinnegan

unread,
Feb 10, 2012, 6:23:03 PM2/10/12
to rubyonra...@googlegroups.com
thank you
Reply all
Reply to author
Forward
0 new messages