在我的 Rails 应用程序中。我正在使用设计。用户会从“ja.myapp.com”、“www.myapp.com”等几个子域进入首页。我只在促销页面内使用几个子域,所以当他们进入“登录”页面时,所有他们将重定向到“www”子域页面。
当用户登录时,如果用户的profile_type是“学生”,用户将重定向到“学生之家”。如果是“教师”,“教师之家”。
当我使用“POW”进行测试时一切正常,但是当我将它上传到生产服务器时。用户登录后,他们都将重定向到 TOP 页面。
我回滚应用程序并尝试再次登录,但同样的事情发生了这发生在我将 heroku 证书从“单域 SSL”上传到“通配符子域 SSL”之后。
在我将 SSL 更改为单域之一后,回滚站点内不会再发生这种情况。
我想这是因为“通配符子域 SSL”和设计会话控制器而发生的,但对此不太确定。
有谁知道解决这个问题?
下面是代码。
路线.rb
root :to => 'students#index', :constraints => lambda { |request| request.env['warden'].user.try(:profile_type) == 'Student' }
root :to => 'teachers#index', :constraints => lambda { |request| request.env['warden'].user.try(:profile_type) == 'Teacher' }
root :to => 'pages#home'
devise_for :users, :controllers => {:sessions => "sessions", :registrations => "registrations", :confirmations => "confirmations"}
配置/初始化程序/session_store.rb
MyAPP::Application.config.session_store :cookie_store, key: '_my_app_secure_session', domain: :all
# coding: utf-8
class SessionsController < Devise::SessionsController
after_filter :clear_sign_signout_flash
def after_sign_in_path_for(resource)
url = root_path
end
def new
@title = I18n.t "sessions.new.title"
super
end
def create
super
end
def destroy
super
end
protected
def clear_sign_signout_flash
if flash.keys.include?(:notice)
flash.delete(:notice)
end
end
end
应用程序控制器
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale, :www_redirect
private
def www_redirect
#if Rails.env.production
parsed_subdomain = request.subdomains.first
locale = Locale.find_by_subdomain(parsed_subdomain)
if (use_www? && parsed_subdomain != "www") || locale.nil?
redirect_to request.url.sub(parsed_subdomain, "www")
end
#end
end
def use_www?
true
end
def set_locale
if user_signed_in?
if current_user.profile_type == "Teacher"
I18n.locale = I18n.default_locale
else
I18n.locale = current_user.locale.i18n_form.to_sym
end
else
if request.subdomains.first == "www" && cookies[:locale]
I18n.locale = cookies[:locale].to_sym
else
I18n.locale = extract_locale_from_subdomain || I18n.default_locale
cookies[:locale] = {
value: I18n.locale.to_s,
expires: 1.year.from_now,
domain: :all
}
end
end
end
def extract_locale_from_subdomain
parsed_subdomain = request.subdomains.first
locale = Locale.find_by_subdomain(parsed_subdomain)
if locale
I18n.available_locales.include?(locale.i18n_form.to_sym) ? locale.i18n_form.to_sym : nil
else
nil
end
end
end
locale_settings_controller.rb
class LocaleSettingsController < ApplicationController
def switch_language
locale = params[:locale]
locale = "www" if locale == "en"
path = params[:uri]
cookies[:locale] = {
value: locale,
expires: 1.year.from_now,
domain: :all
}
if Rails.env.production?
base_url = "http://" + "#{locale}" + ".myapp.com"
else
base_url = "http://" + "#{locale}" + ".myapp.dev"
end
url = path == "/" ? base_url : base_url + "#{path}"
redirect_to url
end
end
生产.rb
config.force_ssl = true
配置/初始化程序/rack_rewrite.rb
require 'rack/rewrite'
LingualBox::Application.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
r301 %r{.*}, 'http://www.myapp.com$&', :if => Proc.new {|rack_env|
rack_env['SERVER_NAME'] == 'myapp.com'
}
end if Rails.env == 'production'