Rails手动从裸域重定向

pre*_*yce 5 dns ruby-on-rails heroku

因此,由于我的托管服务提供商(Heroku)的限制,目前我手动指向裸域.一切正常.问题是如果用户访问mydomain.com/route,重定向将返回到没有/ route的www.mydomain.com.我将如何重新添加路线,但仍然重定向到www.?

class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :ensure_domain
APP_DOMAIN = 'www.domain.com'
def index
end
def ensure_domain
    if Rails.env.production?
        if request.env['HTTP_HOST'] != APP_DOMAIN
            redirect_to "http://#{APP_DOMAIN}", :status => 301
        end
    end
end
end
Run Code Online (Sandbox Code Playgroud)

编辑

我从我的ApplicationController中删除了上面的代码,并选择使用hurikhan77建议的折射gem,这解决了我的问题.

这是我使用的refraction_rules.rb.

Refraction.configure do |req|
  if req.host == "domain.com"
    req.permanent! :host => "www.domain.com"
  end
end
Run Code Online (Sandbox Code Playgroud)

hur*_*n77 5

我建议使用折射宝石:http://rubygems.org/gems/refraction