# Different user sessions with different domain name?

**URL:** <https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277>\
**Category:** rubyonrails-talk\
**Created:** [June 1, 2011, 10:42am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277 "2011-06-01T10:42:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tashfeen\_Ekram](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tashfeen\_Ekram](https://discuss.rubyonrails.org/u/Tashfeen_Ekram)\
**Post date:** [June 1, 2011, 10:42am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/1 "2011-06-01T10:42:08Z")

</div>

i noticed if you access my site with xxx.com versus www.xxx.com separate user sessions are created such that same visitor form the same browser can log in twice. I am not sure if this perhaps is a DNS issue or this can be dealt at the level of rails app.

Thanks.

---

<div class="post-metadata">

**Author:** ![Chris\_Kottom1](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@Chris\_Kottom1](https://discuss.rubyonrails.org/u/Chris_Kottom1)\
**Post date:** [June 1, 2011, 11:09am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/2 "2011-06-01T11:09:21Z")

</div>

Cookies are scoped according to a domain name and path, so if you’re using two different host names, you’re going to get two different cookies.

I would fix this on the level of your web server as it’s got nothing to do with Rails really. This page on the Apache wiki will give you an idea of what you need to do, but the solution will depend on which HTTP server you use.

[http://wiki.apache.org/httpd/CanonicalHostNames](http://wiki.apache.org/httpd/CanonicalHostNames)

---

<div class="post-metadata">

**Author:** ![Jim\_Ruther\_Nill](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jim\_Ruther\_Nill](https://discuss.rubyonrails.org/u/Jim_Ruther_Nill)\
**Post date:** [June 1, 2011, 11:10am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/3 "2011-06-01T11:10:29Z")

</div>

> i noticed if you access my site with [xxx.com](http://xxx.com) versus [www.xxx.com](http://www.xxx.com)
> 
> separate user sessions are created such that same visitor form the
> 
> same browser can log in twice. I am not sure if this perhaps is a DNS
> 
> issue or this can be dealt at the level of rails app.

in config/initializers/session\_store.rb

MyApp::Application.config.session\_store :cookie\_store, :key =\> ‘\_my\_app\_session’, :domain =\> ‘[xxx.com](http://xxx.com)’

so any subdomain will still use the session for [xxx.com](http://xxx.com)

---

<div class="post-metadata">

**Author:** ![Chris\_Kottom1](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@Chris\_Kottom1](https://discuss.rubyonrails.org/u/Chris_Kottom1)\
**Post date:** [June 1, 2011, 11:37am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/4 "2011-06-01T11:37:46Z")

</div>

OK, it’s got _something_ to do with Rails. 🙂

Thanks for the dose of knowledge, Jim.

---

<div class="post-metadata">

**Author:** ![jiblethead](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@jiblethead](https://discuss.rubyonrails.org/u/jiblethead)\
**Post date:** [June 2, 2011, 3:48am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/5 "2011-06-02T03:48:41Z")

</div>

> in config/initializers/session\_store.rb
> 
> MyApp::Application.config.session\_store :cookie\_store, :key =\> '\_my\_app\_session', :domain =\> 'xxx.com'

the solution I've found to work consistently is to modify as above, but set :domain =\> '.xxx.com'

Having the leading period (.) will set a common cookie that is shared by all subdomains. I use this technique to handle a secure subdomain vs. a www subdomain, without creating new sessions between the two.

Kevin

---

<div class="post-metadata">

**Author:** ![Ratnadeep](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@Ratnadeep](https://discuss.rubyonrails.org/u/Ratnadeep)\
**Post date:** [June 2, 2011, 7:53am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/6 "2011-06-02T07:53:30Z")

</div>

But overall redirection from non www to www or vice versa should be done at least for “www” subdomain… As google bot considers www and non www as two different sites.

So go with Apache configs way if you want www and non www to be same.

And go with Rails cookies way, if you really have some subdomains like [app1.example.com](http://app1.example.com) and [app2.example.com](http://app2.example.com), where app1 and app2 are sharing the session.

---

<div class="post-metadata">

**Author:** ![Chris\_Kottom1](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@Chris\_Kottom1](https://discuss.rubyonrails.org/u/Chris_Kottom1)\
**Post date:** [June 2, 2011, 9:10am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/7 "2011-06-02T09:10:58Z")

</div>

Yes, important point there. The choice to host across multiple subdomains should be made for a reason, not by default. If the subdomain is an essential part of the user’s request (Google up “Basecamp style subdomains” if you’re not sure what I mean by this) then it’s possible that you’d want separate cookies for each subdomain. In the vast majority of cases however, the application isn’t inferring anything from the subdomain, and you’re potentially losing PageRank.

Making the configuration at the web server will help you with both problems, if it is in fact a problem to have more than one subdomain, whereas the Rails-only solution helps only with cookie management.

---

<div class="post-metadata">

**Author:** ![Tashfeen\_Ekram](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tashfeen\_Ekram](https://discuss.rubyonrails.org/u/Tashfeen_Ekram)\
**Post date:** [June 5, 2011, 5:48pm UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/8 "2011-06-05T17:48:10Z")

</div>

This has been great advice. I am using nginx. (I know this is now not a rails questions, but I figured to complete the post here so as not to cross post). The way to to do this on nginx is as follows:

server { &nbsp;&nbsp;listen 80; &nbsp;&nbsp;server\_name domain.com \*.domain.com; &nbsp;&nbsp;rewrite ^ [http://www.domain.com$request](http://www.domain.com%24request)\_uri? permanent; }

server { &nbsp;&nbsp;listen 80; &nbsp;&nbsp;server\_name www.domain.com;

&nbsp;&nbsp;index index.html; &nbsp;&nbsp;root /home/domain.com }

This is taken from:

[http://blog.martinfjordvald.com/2010/07/nginx-primer/](http://blog.martinfjordvald.com/2010/07/nginx-primer/)

---

<div class="post-metadata">

**Author:** ![Tashfeen\_Ekram](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tashfeen\_Ekram](https://discuss.rubyonrails.org/u/Tashfeen_Ekram)\
**Post date:** [June 9, 2011, 12:46am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/9 "2011-06-09T00:46:13Z")

</div>

just to make sure i dunerstood this. so to optimize ranking, when soeone types in xxx.com it should be routed to www.xxx.com at the level of the http server?

---

<div class="post-metadata">

**Author:** ![Ratnadeep](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@Ratnadeep](https://discuss.rubyonrails.org/u/Ratnadeep)\
**Post date:** [June 13, 2011, 9:20am UTC](https://discuss.rubyonrails.org/t/different-user-sessions-with-different-domain-name/59277/10 "2011-06-13T09:20:40Z")

</div>

> just to make sure i dunerstood this. so to optimize ranking, when
> 
> soeone types in [xxx.com](http://xxx.com) it should be routed to [www.xxx.com](http://www.xxx.com) at the
> 
> level of the http server?

yes, you are right.
