Take the 2-minute tour ×

Is it possible to do something like this? Or any equivulent? in a DNS server.

([0-9]{2}).mydomain.com. IN CNAME www$1.other-domain.com.

share|improve this question

migrated from stackoverflow.com Apr 19 '10 at 10:10

This question came from our site for professional and enthusiast programmers.

Definitely a server fault I'd say –  Preet Sangha Apr 19 '10 at 2:42
I googled and found NAPTR record, how does it work? –  Kaizoku Apr 19 '10 at 2:46
Your DNS server can reply with whatever it wants for any query. A CNAME record isn't the solution, but a regex-based DNS server is something you could find or write. –  jrockway Apr 19 '10 at 2:46
I don't think you can use NAPTR records, they are reverse DNS records. –  Prof. Moriarty Apr 19 '10 at 10:28

7 Answers 7

up vote 4 down vote accepted

DNS doesn't. But the most popular bind9 nameserver daemon supports this kind of syntax in its zone files:

$GENERATE 1-99 www$ CNAME www$.other-domain.com.

Beware that this is incompatible with other DNS server software.

share|improve this answer
Thanks, this works like wonders :) –  Kaizoku Apr 20 '10 at 2:58

The DNS protocol does not support regular expressions. This said, there is no reason why a name server should not have the capability to evaluate regular expressions internally and serve standard DNS answers.

It won't work with the standard zone format as defined in RFC 1035 section 5 and RFC 1034 section 3.6.1 and I'm not aware of any name server actually implementing support for regular expressions, but basically it is possible.

share|improve this answer

Although regex handling could be written into a DNS server it would be a bad idea to do so.

Any DNS server modified to handle regex expansion is going to have trouble co-existing with the rest of the world. Zone transfers to "standard" servers would either be incomplete, incorrect or absolutely massive, as each permutation would have to be sent as a separate record. Depending on the regex that could result in an almost infinite number of possible records, which is clearly impractical because the transfer may never complete, leaving systems out of sync.

Of course if transfers were only ever to happen between modified servers this might be OK but the universe has a tendency to destroy such simplicity.

share|improve this answer
Then how does wildcard works then? Isn't that "infinite" number of possibilities as well? –  Kaizoku Apr 19 '10 at 11:20
@Kaizoku The * wildcard is a standard DNS feature, and during AXFRs (zone transfers) it is transferred just like that: *, because the receiving server will know what it means. But if you implement DNS regexps in the master server but not in the slave, the situation described by John occurs. –  grawity Apr 19 '10 at 11:31

As jrockway suggested, if you can afford to roll your own nameserver, you have full control over the replies you give. In that case, you could reply with the CNAME record you want, based on the query. I don't know how much DNS traffic you expect to have, how mission critical your web site is, but you could try a DNS server implemented in Perl, using the Net::DNS::Nameserver module.

Alternatively, since your regex is just a shortcut for 100 DNS resource records, you could just run BIND, and have a script generate the zone file you want.

Or, just use your registrar's tools to create the 100 CNAME records.

share|improve this answer

No. But you can have a wildcard:


which won't quite do what you want, but maybe you can do some server-side validation to ensure that the hostname is valid? Won't help with re-writing to a different A record though...

share|improve this answer
But that is many-to-one I think. Does wildcard work on the other domain as well? –  Kaizoku Apr 19 '10 at 3:20
Correct, it is many:one - and no, there's no wildcard on the other domain. DNS is not designed for this sort of functionality, but if you wanted to modify the DNS daemon (say bind) I'm sure you could add this functionality in. –  Mark Henderson Apr 19 '10 at 3:28

No regex, just wildcard domain names, e.g. *.mydomain.net - (RFC 4592, especially section 2)

share|improve this answer

You can't do this with standard DNS software.

It is possible if you use a custom DNS server which recognises that particular pattern and then dynamically synthesises the correct response. It would actually be trivial for example using my evldns server framework. If you know 'C' it should take less than an hour to implement.

Note however that all authoritative DNS servers for the domain would need to have this feature. Whilst a wildcard record can be happily transferred from primary to secondary servers using AXFR, that only works because it's built into the DNS protocol.

share|improve this answer

Your Answer


