I have been searching for information on this topic for a couple days and I keep running into road blocks.
I have a Django web site and application running at www.example.com
and I'm forcing HTTPS. It's deployed on Apache 2.2 with WSGI. This works fine and works for both example.com
and www.example.com
.
I also have a REST API (pip install djangorestframework
) running at https://www.example.com/api/v1/
. This also works fine.
I want to run the API from a subdomain https://api.example.com
and keep this URL in the address bar. For example, to fetch JSON objects I might use something like this:
curl -X GET https://api.example.com/objects/ -H 'Authorization: Token xxx'
I can get this now by using this:
curl -X GET https://www.example.com/api/v1/objects/ -H 'Authorization: Token xxx'
I have a separate SSL certificate for this subdmain and his has been correctly configured.
I have tried many things in my Apache configuration to accomplish this but failed at every turn. I thought I could use mod_rewrite
to silently fetch the content from https://www.example.com/api/v1/
while leaving https://api.example.com
in the address bar. Is this possible? Here is what I've tried (in the sites-available
virtual host file):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^api.example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/api/v1/$1 [L]
I have tried several variations of this idea to no avail. I played around with HTTPS on/off
as well with no real benefit.
I read a couple places that using mod_proxy
could accomplish this but when I went down this road, the API was available (after quite a bit of tweaking) at the desired URL (https://api.example.com
) but none of my static content was there and when I clicked on a relative link in the Django REST Framework UI, I'd get 404s because it was looking at:
https://api.example.com/api/v1/
which Django complained about: /api/v1/api/v1/
I guess all I'm trying to do is make https://api.example.com
the base URL for the API as if it were https://www.example.com/api/v1/
.
Duplicate that lead to the discovery of the
django-hosts
package:Django subdomain configuration for API endpoints
I have been playing around with this and it shows promise, although I haven't "solved" my problem yet. I plan to edit this answer once I get more information to share. In the meantime, if anyone has used
django-hosts
to approach my original question, please add your answers here or at least make some comments!