Different URLCONF for different domains

177 views
Skip to first unread message

Guy Tamir

unread,
Mar 12, 2014, 8:57:50 AM3/12/14
to django...@googlegroups.com
Hey all,

I have a Django app that serves to multiple domains.
In the primary domain (lets call it primary.com) I have some page that are server via the path - primary.com/a/<key>
I want users from another domain (lets call it secondary.com)  to be able to get to this path : primary.com/a/<key> by entering secondary.com/<key>
(Both domains have a CNAME record pointing to the same server)

Is there a way to reconfigure the URLCONF for specific domains so that it will automatically go to a different urls file?

Best,
Guy

Shawn Milochik

unread,
Mar 12, 2014, 12:43:28 PM3/12/14
to django...@googlegroups.com
URLs will be processed in the order they appear in your urls.py file.

If one of your first entries in urls.py is an "include" that handles all URLs that start with "/a/", then all those requests will be handled by the URL patterns in the included URLs.py.

Then, all URLs that don't get "caught" by that pattern will be checked against the rest of the patterns in your urls.py until it finds a match.

According to the docs[1], urls.py has no access to the domain in question, so this is a bit of a workaround. 


Guy Tamir

unread,
Mar 12, 2014, 1:06:52 PM3/12/14
to django...@googlegroups.com, Sh...@milochik.com
Hey,

I'm aware of what your saying but I'd I like to reconfigure in a way similar to django-subdomains only instead of specifying the sub domain I'll specify the domain..
Is there a good hack you can think of that can help me in this case?

Russell Keith-Magee

unread,
Mar 12, 2014, 11:52:27 PM3/12/14
to Django Users
 There are at least two options.

1) Deploy two different sites from the same codebase, with different settings files. The ROOT_URLCONF is a just a setting; if you only have a small, fixed number of subdomains, you may find it easiest to deploy a single version of your codebase, and have a different settings file for each subdomain. You then set up your web server to catch each domain and use the right settings file for each domain.

2) You can modify the URLconf as part of a middleware. Under this approach, you write a custom middleware that handles the request, and sets `request.urlconf` depending on the value of request.domain. request.domain contains the full domain for the site; request.urlconf controls which URLConf is used to resolve routing requests once middleware request processing is completed, overriding ROOT_URLCONF. This is effectively the same thing that django-subdomains does, but gives you access to the full domain, not just a subdomain.

Ideally (from the point of view of the wider community), you would fork django-subdomains and provide the extra functionality to allow for control over full domains - but that's probably a bigger piece of work.

I hope that helps.

Yours,
Russ Magee %-)



Reply all
Reply to author
Forward
0 new messages