Question: Sorry if the Question Title is a bit off, couldn't think of something more descriptive.

So I have 2 Domains: `aaa.com` and `bbb.com`.

I want my second domain `bbb.com` to redirect always to `aaa.com` EXCEPT IF it has a certain path: `bbb.com/ct/:id`

Both domains right now hit the same Heroku App.

So in my `Application Controller` I guess, or Routes I, have to scan the request URL and if it contains `/ct/:id` let it continue to the controller action, if not redirect to aaa.com.

Or can I somehow Set up the the DNS to only redirect if the `ct/:id` is not in the url ?

Basically I want to achieve that a User can't navigate the App from `bbb.com` except if the specific url `bbb.com/ct/:id` is present.

I'm using GoDaddy as a Registar and have 2 Cnames set up to my Heroku Apps.

How do I accomplish this behavior ?

Answer: 
I prefer to do your redirects in an actual controller and not in the route file so you can write a controller spec for it.

```
# app/controllers/application_controller.rb

before_action :redirect_bbb_to_aaa

def redirect_bbb_to_aaa
  if request.host == "http://bbb.com"
    redirect_to some_aaa_path unless request.url == "http://bbb.com/ct/:id"
  end
end

```
You can create a rack middleware to do the redirects. One such example is here <https://github.com/gbuesing/rack-host-redirect/blob/master/lib/rack/host_redirect.rb>

OR

Use constraint in routes.rb.
Note: I have not tested it. 

```
get "*path" => redirect("http://aaa.com"), constraint: lambda { |request| !request.url.match(/ct\/\d/) }

```