22

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?

2
  • Have you considered DNS forwarding?
    – Anil
    Jun 6, 2012 at 16:55
  • you can do it in application_controller: if request.host == 'some.site' redirect_to url_for(host: 'site', subdomain: 'another'), status: :moved_permanently end Oct 20, 2016 at 8:30

6 Answers 6

44

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
9
  • For me this worked everywhere except the home page. The solution is good enough for me. Thank you very much. Oct 12, 2012 at 20:33
  • 1
    The splat rout by itself matches a nonempty string, so root URL won't be matched. Just add the parentheses: match "/(*path)"...
    – Janko
    May 15, 2013 at 16:25
  • 1
    Rails 4: add via: [:get, :post] to the match options
    – meatrobot
    Nov 4, 2013 at 23:48
  • 8
    Use // instead of http:// to ensure the redirect is protocol independent and works for http as well as for https. Feb 27, 2014 at 10:01
  • Curious as to why not use: "via: [:get, :post, :put, :patch, :delete]" Mar 12, 2015 at 0:00
6

similar to other answers, this one worked for me:

# config/routes.rb
constraints(host: "foo.com", format: "html") do
  get ":any", to: redirect(host: "bar.com", path: "/%{any}"), any: /.*/
end
6

This does the job of the other answer. Though in addition, it preserves query strings as well. (Rails 4):

# http://foo.tld?x=y redirects to http://bar.tld?x=y
constraints(:host => /foo.tld/) do
  match '/(*path)' => redirect { |params, req|
    query_params = req.params.except(:path)
    "http://bar.tld/#{params[:path]}#{query_params.keys.any? ? "?" + query_params.to_query : ""}"
  }, via: [:get, :post]
end

Note: If you're dealing with full domains instead of just subdomains, use :domain instead of :host.

1
  • 2
    Actually "http://bar.tld#{req.fullpath}" because req.fullpath already contains the leading forward slash.
    – zelanix
    Jun 4, 2015 at 21:53
3

The following solution redirects multiple domains on GET and HEAD requests while returning http 400 on all other requests (as per this comment in a similar question).

/lib/constraints/domain_redirect_constraint.rb:

module Constraints
  class DomainRedirectConstraint
    def matches?(request)
      request_host = request.host.downcase
      return request_host == "foo.tld1" || \
             request_host == "foo.tld2" || \
             request_host == "foo.tld3"
    end
  end
end

/config/routes.rb:

require 'constraints/domain_redirect_constraint'

Rails.application.routes.draw do
  match "/(*path)", to: redirect {|p, req| "//bar.tld#{req.fullpath}"}, via: [:get, :head], constraints: Constraints::DomainRedirectConstraint.new
  match "/(*path)", to: proc { [400, {}, ['']] }, via: :all, constraints: Constraints::DomainRedirectConstraint.new

  ...
end

For some reason constraints Constraints::DomainRedirectConstraint.new do didn't work for me on heroku but constraints: Constraints::DomainRedirectConstraint.new worked fine.

2

Bit more modern approach:

constraints(host: 'www.mydomain.com') do
  get '/:param' => redirect('https://www.mynewurl.com/:param')
end
1
  • 1
    Good answer, but (in Rails 5, at least) the param needs to be included in %{}. This would give you: get '/:param', to: redirect('https://www.mynewurl.com/%{param}') Dec 16, 2019 at 12:27
1
constraints(host: /subdomain\.domain\.com/) do
  match '/(*path)' => redirect { |params, req|
    "https://www.example.com#{req.fullpath}"
  }, via: [:get, :head]
end

I use this when using custom domains on Heroku and I want to redirect from the myapp.herokuapp.com -> www.example.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.