Route a subdomain to a different server


Problem:

Steffen Funke
Jul 21, 2017 · 2 min read

You own a Domain mydomain.com, which points to a Server (let’s say 111.111.111.111). You now want a subdomain, e.g. dev.mydomain.com point to a different server 222.222.222.222.

This may be useful if you own a couple of development servers with unique IP addresses, and you want to provide pretty URLs to the client to test your webservice (that was my usecase).


Solution:

First you need to log into your Domain provider, mine was gandi.net. This is different depending on where you have your Domain registered.

Find your domain Zone file.

Edit the zone file, mine looked like this before I made the changes:

@ 3h IN A 111.111.111.111
* 3h IN CNAME mydomain.com.

The first line basically says “Route all Requests for $ORIGIN (the @ sign, is replaced with mydomain.com implicitely) to 111.111.111.111
https://serverfault.com/questions/83874/whats-the-meaning-of-in-a-dns-zone-file

The second line is a wildcard for all subdomains (*), which should be forwarded to mydomain.com
Note the trailing period after mydomain.com
— This is important since it marks an Fully Qualified Domain name. If you had missed this period, your $ORIGIN (mydomain.com) would be implicitely appended, so the entry would look like:

* 3h IN CNAME mydomain.com.mydomain.com

which you clearly see can lead into a debugging hell quickly. More infos on that here: https://serverfault.com/questions/18113/dns-trailing-periods

Add a new line after the first line, so the zone file looks like this:

@ 3h IN A 111.111.111.111
dev 30m IN A 222.222.222.222
* 3h IN CNAME mydomain.com.

This new line says, take all requests to the dev subdomain (and this subdomain only), and route them to 222.222.222.222

Note: I decreased the TTL (Time-to-live) time to 30m for this subdomain. You don’t have to do that.

Save your zone file, and set it as current version (again, may be different depending on your Domain provider)

Now just wait a bit until the changes have been propagated, Should be working within the next hour.

🍻


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade