Ruby on rails Rails:重定向到特定域。。。但是不要';不覆盖SSL?
因此,我正在将我的rails(3.0.9)应用程序从一个域移动到另一个域。Heroku建议在应用程序控制器中使用before_过滤器,以确保每个人都进入新域,如下所示:Ruby on rails Rails:重定向到特定域。。。但是不要';不覆盖SSL?,ruby-on-rails,ruby-on-rails-3,ssl,request,Ruby On Rails,Ruby On Rails 3,Ssl,Request,因此,我正在将我的rails(3.0.9)应用程序从一个域移动到另一个域。Heroku建议在应用程序控制器中使用before_过滤器,以确保每个人都进入新域,如下所示: before_filter :ensure_domain if Rails.env.production? APP_DOMAIN = 'www.newdomain.com' def ensure_domain if request.env['HTTP_HOST'] != APP_DOMAIN redirect_t
before_filter :ensure_domain if Rails.env.production?
APP_DOMAIN = 'www.newdomain.com'
def ensure_domain
if request.env['HTTP_HOST'] != APP_DOMAIN
redirect_to "http://#{APP_DOMAIN}", :status => 301
end
end
然而,在某些控制器视图上,我使用的是ssl\u需求
,我相信这也会起到同样的作用,但会强制使用ssl协议
我不太擅长处理请求和其他事情。我的问题是,这两个是否将创建一个无限循环,其中SLL尝试重定向到https,而过滤器尝试将其放回http之前
您将如何解决此问题?请遵守当前协议:
redirect_to("#{request.protocol}#{APP_DOMAIN}", :status => 301)
对于一个具有一定扩展性的综合答案,总体上看起来是这样的
class ApplicationController < ActionController::Base
before_filter :redirect_to_example if Rails.env.production?
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
private
# Redirect to the appropriate domain i.e. example.com
def redirect_to_example
domain_to_redirect_to = 'example.com'
domain_exceptions = ['example.com', 'www.example.com']
should_redirect = !(domain_exceptions.include? request.host)
new_url = "#{request.protocol}#{domain_to_redirect_to}#{request.fullpath}"
redirect_to new_url, status: :moved_permanently if should_redirect
end
end
class ApplicationController
这会将所有内容重定向到域\u到\u重定向到
,除了域中的内容例外