A small Go command-line utility to rewrite the hostname component of a URI using DNS CNAME records.
uri-rewriter hostname-cname [--cname-level N] <uri>--cname-level, -l: The step in the CNAME chain to use for replacement.1= the first CNAME target (closest to the original hostname)- If the requested level exceeds the available CNAME chain length, the last available CNAME target is used.
- If there is no CNAME for the hostname, the URI is output unchanged.
Assume DNS has the following CNAME chain:
www.example.com -> www.example.net -> origin.example.org
- Level 1 (first CNAME):
uri-rewriter hostname-cname -l 1 "https://www.example.com/path?x=1"
# https://www.example.net/path?x=1- Level 2 (second CNAME):
uri-rewriter hostname-cname -l 2 "https://www.example.com/path?x=1"
# https://origin.example.org/path?x=1- With port preservation:
uri-rewriter hostname-cname -l 1 "https://www.example.com:8443/path"
# https://www.example.net:8443/path- Only the rewritten URI is printed to stdout. Errors (e.g., invalid URI, DNS failures) are written to stderr with a non-zero exit code.
- The tool follows the system resolver configuration (
/etc/resolv.conf) to query CNAME records. If it is unavailable, it falls back to public resolvers (Cloudflare1.1.1.1and Google8.8.8.8). - Trailing dots in DNS names (e.g.,
example.com.) are removed in the output for URI compatibility.
git clone https://github.com/dpc-sdp/uri-rewriter.git
cd uri-rewriter
go build .
# The binary will be at ./uri-rewriterOr install directly with Go:
go install github.com/dpc-sdp/uri-rewriter@latestMIT