module ActionDispatch::Routing::Redirection

def redirect(*args, &block)


get 'accounts/:name' => redirect(SubdomainRedirector.new('api'))

a string.
common redirect routes. The call method must accept two arguments, params and request, and return
Finally, an object which responds to call can be supplied to redirect, allowing you to reuse

/stories, /stories?foo=bar, redirect to /posts and /posts?foo=bar respectively.
for example the +path+ option in the last example.
This will redirect the user, while changing only the specified parts of the request,

get '/stories', to: redirect(path: '/posts')
get 'stores/:name(*all)', to: redirect(subdomain: 'stores', path: '/%{name}%{all}')
get 'stores/:name', to: redirect(subdomain: 'stores', path: '/%{name}')

to change, it also supports interpolation of the path similar to the first example.
The options version of redirect allows you to supply only the parts of the URL which need

the block to +get+ instead of +redirect+. Use { ... } instead.
Note that the do end syntax for the redirect block wouldn't work, as Ruby would pass

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

return value.
params, depending of how many arguments your block accepts. A string is required as a
the redirect in question. Either the params and request are supplied as arguments, or just
The block version of redirect allows for the easy encapsulation of any logic associated with

Alternatively you can use one of the other syntaxes:

a mounted engine or where the application is deployed to a subdirectory of a website.
current SCRIPT_NAME environment variable. This is typically '/' but may be different in
Note that if you return a path without a leading slash then the URL is prefixed with the

get 'docs/:article', to: redirect('/wiki/%{article}')

You can also use interpolation in the supplied redirect argument:

get "/stories" => redirect("/posts", status: 307)

default. This can be overridden with the +:status+ option:
The redirect will use a 301 Moved Permanently status code by

/stories, /stories?foo=bar, etc all redirect to /posts.
This will redirect the user, while ignoring certain parts of the request, including query string, etc.

get "/stories" => redirect("/posts")

Redirect any path to another path:
def redirect(*args, &block)
  options = args.extract_options!
  status  = options.delete(:status) || 301
  path    = args.shift
  return OptionRedirect.new(status, options) if options.any?
  return PathRedirect.new(status, path) if String === path
  block = path if path.respond_to? :call
  raise ArgumentError, "redirection argument not supported" unless block
  Redirect.new status, block
end