I have an app that is roughly fractured between a server serving front end and one serving an API. We'd like to add a wildcard to catch usernames on the subdomain and direct those to the front end server. So my URLs map like so:
Server that serves front-end files is mapped by https://example.com
and https://www.example.com
Server for the API is mapped by https://api.example.com
We also have a forum, demo, etc at domains like https://forum.example.com
and https://demo.example.com
Right now this is all straightforward using ALIAS records in DNSimple.
What is the best way to capture https://username.example.com
and map it to the front-end server? We will need it to be a wildcard e.g. *.example.com
, but we don't want to direct api
, forum
, demo
, etc to that server.
Is there a way to set hierarchies or priorities at the DNS level so that it checks for the named matches first and then moves on to wildcard if nothing is found?
The way to go is exactly the one you considered. You should add a wildcard DNS record
*.example.com
that points to the IP (A) or hostname (CNAME) you want. The rule is that if there is a specific record matching the query, our DNS server will server it. Otherwise, we will fallback to the wildcard.
In other words, if you have a wildcard in place and a client asks for api.example.com
, we will serve the api
DNS record and not the wildcard. Specific records have higher priorities over wildcards.