Rails routes redirect for query string
Building on Alejandra's answer, more verbose but without the ?
if there's no query string:
get "/old/report/:client", to: redirect{ |params, request| ["/new/report/#{params[:client]}", request.query_string.presence].compact.join('?') }
So /old/report/:client?with=param
will become /new/report/:client?with=param
,
and /old/report/:client
will become /new/report/:client
.
The question mention by Matt helped me to figure out my answer (thanks a lot!). It was a slightly different for my specific case. Im leaving the answer that worked for me for future reference.
match "/old/report/:client" => redirect{ |params, request| "/new/report/#{params[:client]}?#{request.query_string}" }
Modify redirect
to use the path:
option to preserve the querystring:
- get '/old/about', to: redirect('/new/about')
+ get '/old/about', to: redirect(path: '/new/about')
This is demonstrated in the API docs for redirect
, see http://api.rubyonrails.org/classes/ActionDispatch/Routing/Redirection.html#method-i-redirect