Rails - 将所有没有子域的请求转发到子域?

Rails - forward all requests without subdomain to a subdomain?

如何将所有没有子域的请求转发到子域?

我希望所有 domain.com 请求转到 api.domain.com

它用于 angular 应用程序,它调用 get "/resources" 等。所以 api.domain。com/resources 有效,但域。com/resources 不

rails 4

在应用程序控制器中作为 before_action

    unless request.url.to_s.include? 'api'
        redirect_to subdomain: 'api' and return
    end