My Host

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?

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?

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

thank you

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs