Question

I have been having inconsistent behavior with my Django app.

If I login with no www, and then prepend www, it's not authenticated, and all the combinations thereof. (www.mydomain.com and mydomain.com like different sites in terms of auth)

If the authentication code is important, I wrote a middleware based on the tutorial here: http://onecreativeblog.com/post/59051248/django-login-required-middleware

So far I have fixed the issue forcing the appending of www, using PREPEND_WWW = True, but I would still like to understand the issue;)

Does anyone have an idea of what may be going on? Thanks in advance!

Was it helpful?

Solution

What Zaha Zorg said: Cookies from Django won't work for both a prepended www and non-www domain by default.

However, the deeper issue here is that you're allowing both www and non-www domains of your site to serve identical content. Besides the obvious SEO consequences of having traffic split between the two, you run into issues like these. The proper way to handle this is to redirect all traffic from one to the other (whichever you prefer). The PREPEND_WWW setting you found works perfectly for this. For the opposite (forcing all traffic to non-www), it's recommended to just do a re-write at the server configuration level, such as Apache or Nginx.

OTHER TIPS

You need to look at https://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#session-cookie-domain

SESSION_COOKIE_DOMAIN Default: None

The domain to use for session cookies. Set this to a string such as ".lawrence.com" for cross-domain cookies, or use None for a standard domain cookie. See the How to use sessions.

Could it be that cookies depend on the hostname of the server ? This could explain why both domain names are considered different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top