← back to overview

Share a session with multiple subdomains in Rails

Multi-subdomain sessions can be easily enabled in rails. If you're already using a persistent/shared session store (like the database-backed ActiveRecordStore) then all you need to do is to tell rails to turn on shared sessions by adding this line to your environment.rb.

ActionController::Base.session = { :domain => '.mydomain.com' }

Note the leading dot in ".mydomain.com". — It's even possible to share a session between different applications. Just make shure that all apps have the same session secret configured and use the same session store / database.

config.action_controller.session_store = :active_record_store
config.action_controller.session = {
:session_key => '_MYWEBAPP', # has to match on all apps
:secret      => 'SECRETKEY'  # has to match on all apps
}

(Apparently this only works for rails >= 2.3)