Redirect to same server with different port

22 views
Skip to first unread message

Jared S.

unread,
Sep 3, 2014, 11:07:53 AM9/3/14
to rubyonra...@googlegroups.com
I'm trying to set up a route to redirect to my monit interface, but I'm
not sure how to do it. what I'd like is my https://domain.com:3000/monit
to redirect to http://domain.com:2812 .
I tried:

get "/monit" => redirect("#{request.protocol}#{request.host}:2812")

in my config.rb, but it doesn't seem to know about request, base_url or
anything else I can find.
Any help is greatly appreciated!

--
Posted via http://www.ruby-forum.com/.

Matt Jones

unread,
Sep 4, 2014, 10:16:40 AM9/4/14
to rubyonra...@googlegroups.com


On Wednesday, 3 September 2014 11:07:53 UTC-4, Ruby-Forum.com User wrote:
I'm trying to set up a route to redirect to my monit interface, but I'm
not sure how to do it. what I'd like is my https://domain.com:3000/monit
to redirect to http://domain.com:2812 .
I tried:

get "/monit" => redirect("#{request.protocol}#{request.host}:2812")

in my config.rb, but it doesn't seem to know about request, base_url or
anything else I can find.


Using double-quotes here will not do what you want - they get evaluated when the routes file is loaded, not per-request. For this case, the block form of redirect is probably what you want:

get 'jokes/:number', to: redirect { |params, request|
  path = (params[:number].to_i.even? ? "wheres-the-beef" : "i-love-lamp")
  "http://#{request.host_with_port}/#{path}"
}

See the docs for ActionDispatch::Routing::Redirection for more info.

--Matt Jones
Reply all
Reply to author
Forward
0 new messages