Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Rails routes to redirect from one domain to another?

Tags:

My app used to run on foo.tld but now it runs on bar.tld. Requests will still come in for foo.tld, I want to redirect them to bar.tld.

How can I do this in rails routes?

like image 845
John Bachir Avatar asked Jun 06 '12 16:06

John Bachir


People also ask

How do I redirect in rails?

Rails's redirect_to takes two parameters, option and response_status (optional). It redirects the browser to the target specified in options. This parameter can be: Hash - The URL will be generated by calling url_for with the options.

How do routes work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

What is routes RB in rails?

It's a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules. Best of all, Rails' Routing works with any web server. Routes are defined in app/config/routes. rb.


1 Answers

This works in Rails 3.2.3

constraints(:host => /foo.tld/) do   match "/(*path)" => redirect {|params, req| "http://bar.tld/#{params[:path]}"} end 

This works in Rails 4.0

constraints(:host => /foo.tld/) do   match "/(*path)" => redirect {|params, req| "http://bar.tld/#{params[:path]}"},  via: [:get, :post] end 
like image 177
Lukas Eklund Avatar answered Sep 22 '22 21:09

Lukas Eklund