How to connect a domain to a server: DNS records, www and a provider move without downtime

You set A and AAAA on the apex and a CNAME on www, pick one version of the address with a 301 redirect, check the answers of several resolvers and move DNS to another provider so that neither the site nor the email disappears. We ran the commands against real domains and two local DNS servers.

What you will do

A domain works when the registry points to its name servers, the NS records, and those answer with records that lead to the web and mail servers. After this guide the apex and www open the same site, one version redirects to the other, and changing DNS providers causes no downtime.

DNS propagation is really caching. A resolver keeps an answer for the time in the record's TTL and only then asks again, so a move starts with lowering the TTL, not with changing the address.

Level
beginner
Code
Bash
Steps
9 steps
  1. DNS Records A and AAAA on the apex, a CNAME on www.
  2. nginx One version of the address A 301 from www to the apex, path included.
  3. TTL Preparation A 300-second TTL before any change.
  4. Result A move without downtime The new zone checked before you change NS.

What you need

  • Access to the registrar and the DNS provider

    The registrar changes the domain's NS servers and the DNS provider keeps its records. They are often the same company, but they do not have to be: github.com, for example, has its zone with two providers at once.

  • The server's IPv4 address, and IPv6 if it has one

    The AAAA record tells IPv6 users where to connect, so the IPv6 address must really serve the site. The examples use 192.0.2.10 and 2001:db8::10 from ranges reserved for documentation.

  • A terminal with dig and curl

    On Ubuntu and Debian dig comes in the bind9-dnsutils package. We ran dig 9.18.39 and curl 8.5.0.

  • A certificate covering both names

    A redirect from https://www works only when the certificate covers www. Without it the browser shows a connection error before it ever gets the 301.

The records that connect a domain to a server

RecordWhat it doesExample
Athe server's IPv4 addressexample.com. 3600 IN A 192.0.2.10
AAAAthe server's IPv6 addressexample.com. 3600 IN AAAA 2001:db8::10
CNAMEan alias for another name, never on the apexwww.example.com. 3600 IN CNAME example.com.
NSthe zone's name servers, changed at the registrarexample.com. NS ns1.example.com.
MXthe mail server, which stays as it is when the site movesexample.com. 3600 IN MX 10 mail.example.com.

Step by step

Each step with commands to copy. The list on the left shows where you are.

01

Check who serves the domain's DNS

dig NS shows the name servers, which is where you change the records. dig +noall +answer prints just the answers, with the TTL in the second column. www.github.com is a CNAME to github.com, and cloudflare.com has two AAAA records, so two IPv6 addresses.

Before you change anything, write the current records down: A, AAAA, CNAME, MX and every TXT. During a move you compare the new zone against this list.

dig, github.com and cloudflare.com
Command
dig +short NS github.com
Output
dns2.p08.nsone.net.dns3.p08.nsone.net.dns4.p08.nsone.net.ns-421.awsdns-52.com.ns-520.awsdns-01.net.ns-1283.awsdns-32.org.ns-1707.awsdns-21.co.uk.dns1.p08.nsone.net.
Command
dig +noall +answer www.github.com
Output
www.github.com. 429 IN CNAME github.com.github.com. 32 IN A 140.82.121.4
Command
dig +noall +answer cloudflare.com AAAA
Output
cloudflare.com. 178 IN AAAA 2606:4700::6810:85e5cloudflare.com. 178 IN AAAA 2606:4700::6810:84e5
02

Set A and AAAA on the apex and a CNAME on www

The apex gets A and AAAA records with the server's addresses. www gets a CNAME to the apex, so when the server changes you fix the address in one place only. A resolver asking for www gets both records straight away: the alias and the address.

We published the records below on a local DNS server and queried it with dig, hence @127.0.0.1 -p 15353 in the commands. On your side you ask your provider's name server, such as @ns1.example.net, or leave out @ to ask the default resolver.

example.com.zone
; site: IPv4 address, IPv6 address and www as an alias of the apexexample.com. 3600 IN A 192.0.2.10example.com. 3600 IN AAAA 2001:db8::10www.example.com. 3600 IN CNAME example.com.
dig, a local test zone
Command
dig @127.0.0.1 -p 15353 +noall +answer example.com A
Output
example.com. 3600 IN A 192.0.2.10
Command
dig @127.0.0.1 -p 15353 +noall +answer example.com AAAA
Output
example.com. 3600 IN AAAA 2001:db8::10
Command
dig @127.0.0.1 -p 15353 +noall +answer www.example.com A
Output
www.example.com. 3600 IN CNAME example.com.example.com. 3600 IN A 192.0.2.10
03

Do not put a CNAME on the apex

RFC 1034 and 2181 say that a name with a CNAME record cannot have any other records. The apex always has SOA and NS, and usually MX and TXT too, so a CNAME does not belong there.

A DNS server will not always stop you. CoreDNS published a CNAME on example.net for us without complaint, and then answered the MX and TXT questions with the CNAME as well. A resolver then looks for the mail server and the SPF record in someone else's domain, shop.example.org.

If the host gives you only a name and no IP address, use your DNS provider's feature that turns a CNAME into an address. Cloudflare calls it CNAME flattening: the server finds the target's IP address itself and returns it instead of the CNAME record.

dig, a local zone with the mistake
Command
dig @127.0.0.1 -p 15353 +noall +answer example.net A
Output
example.net. 3600 IN CNAME shop.example.org.
Command
dig @127.0.0.1 -p 15353 +noall +answer example.net MX
Output
example.net. 3600 IN CNAME shop.example.org.
Command
dig @127.0.0.1 -p 15353 +noall +answer example.net TXT
Output
example.net. 3600 IN CNAME shop.example.org.
04

Pick one version of the address and redirect the other

Both versions are fine: github.com redirects www to the bare domain and cloudflare.com does the opposite, as the output shows. What matters is that the site has one address and the other version sends visitors there with a 301 that keeps the path and parameters. In nginx $request_uri does that.

The configuration below catches three cases: http on the apex, http on www and https on www. Each ends in a single redirect to https://example.com, with no chain of several 301s. We tested it in nginx 1.31.6 in Docker, and curl with --connect-to and --resolve sent the requests to that container instead of a real server.

/etc/nginx/conf.d/example.com.conf
server { listen 80; server_name example.com www.example.com; return 301 https://example.com$request_uri;}server { listen 443 ssl; server_name www.example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; include /etc/nginx/snippets/security-headers.conf; return 301 https://example.com$request_uri;}
curl: local nginx, github.com and cloudflare.com
Command
curl -sI --connect-to example.com:80:127.0.0.2:18080 'http://example.com/pricing?plan=yearly' | grep -i -E '^(HTTP|location)'
Output
HTTP/1.1 301 Moved PermanentlyLocation: https://example.com/pricing?plan=yearly
Command
curl -sI --connect-to www.example.com:80:127.0.0.2:18080 'http://www.example.com/pricing?plan=yearly' | grep -i -E '^(HTTP|location)'
Output
HTTP/1.1 301 Moved PermanentlyLocation: https://example.com/pricing?plan=yearly
Command
curl -skI --resolve www.example.com:443:127.0.0.2 'https://www.example.com/pricing?plan=yearly' | grep -i -E '^(HTTP|location|strict)'
Output
HTTP/1.1 301 Moved PermanentlyLocation: https://example.com/pricing?plan=yearlyStrict-Transport-Security: max-age=31536000; includeSubDomains
Command
curl -sI http://github.com/features | grep -i -E '^(HTTP|location)'
Output
HTTP/1.1 301 Moved PermanentlyLocation: https://github.com/features
Command
curl -sI https://www.github.com/features | grep -i -E '^(HTTP|location)'
Output
HTTP/2 301 location: https://github.com/features
Command
curl -sI https://cloudflare.com/learning/ | grep -i -E '^(HTTP|location)'
Output
HTTP/2 301 location: https://www.cloudflare.com/learning/
05

Check the answer with several resolvers

The domain's name server, here ns1.hosti24.pl for vulcode.com, always gives the full TTL from the record: 3600 seconds. A public resolver gives what is left in its cache. 1.1.1.1 had held the answer for a while and was keeping it for another 1051 seconds. 8.8.8.8 and 9.9.9.9 showed the full 3600, so they had just fetched the record.

That is all propagation is: after a record changes, every resolver shows the new value at the latest once the TTL it remembered runs out. Asking the name server directly tells you what is in the zone. Asking resolvers tells you what users see.

dig, vulcode.com
Command
dig +short NS vulcode.com
Output
ns2.hosti24.pl.ns1.hosti24.pl.
Command
dig @ns1.hosti24.pl +noall +answer vulcode.com A
Output
vulcode.com. 3600 IN A 89.144.32.174
Command
dig @1.1.1.1 +noall +answer vulcode.com A
Output
vulcode.com. 1051 IN A 89.144.32.174
Command
dig @8.8.8.8 +noall +answer vulcode.com A
Output
vulcode.com. 3600 IN A 89.144.32.174
Command
dig @9.9.9.9 +noall +answer vulcode.com A
Output
vulcode.com. 3600 IN A 89.144.32.174
06

Lower the TTL before any address change

Change the TTL of the site's records to 300 seconds and leave the addresses alone for now. Wait as long as the old TTL, an hour here: by then no resolver holds a copy with TTL 3600. After the address changes everyone sees the new server within five minutes, and if something goes wrong, going back also takes five minutes.

Once the new server works, restore the longer TTL. A short TTL means more queries to the name servers but breaks nothing.

example.com.zone
; before the move: TTL 300 seconds, addresses unchangedexample.com. 300 IN A 192.0.2.10example.com. 300 IN AAAA 2001:db8::10www.example.com. 300 IN CNAME example.com.
dig, a local test zone
Command
dig @127.0.0.1 -p 15353 +noall +answer example.com A
Output
example.com. 300 IN A 192.0.2.10
Command
dig @127.0.0.1 -p 15353 +noall +answer www.example.com A
Output
www.example.com. 300 IN CNAME example.com.example.com. 300 IN A 192.0.2.10
07

Test the new server before you change DNS

curl --resolve connects to the IP address you give it but sends the real domain name, in TLS as well. That lets you check the certificate, the redirects and the site on the new server before you change a single record. In a browser an entry in /etc/hosts does the same.

Below, github.com answers 200 at its real address, while at an address from the documentation range curl ends with error 28, a connection timeout. That result on a new server means a firewall or a service that is not listening yet.

curl --resolve, github.com
Command
dig +short github.com A
Output
140.82.121.4
Command
curl -sI --resolve github.com:443:140.82.121.4 https://github.com/ | head -n 1
Output
HTTP/2 200
Command
curl -sI --resolve github.com:443:192.0.2.10 --connect-timeout 5 https://github.com/ ; echo "curl exit: $?"
Output
curl exit: 28
08

Move the zone to the new provider and compare

When you change DNS providers you copy the whole zone: the site's records, MX, every TXT with SPF, DKIM, DMARC and service verifications, and CAA and SRV if you have them. The new provider answers queries for your domain before you change NS, so you can ask its server directly.

The script below reads a list of names and types from a file, asks both servers and prints = for a matching answer or != with the difference. In our test the new zone was missing the DKIM key. After the NS change mail would still flow, but the DKIM signature would stop passing.

dns-compare.sh
#!/usr/bin/env bash# Compares the answers of the old and the new DNS provider before you change the NS servers at the registrar.# Usage: ./dns-compare.sh "@old-ns" "@new-ns" < records.txt (one name and type per line)old=($1)new=($2)while read -r name type; do [ -z "$name" ] && continue a=$(dig "${old[@]}" +short "$name" "$type" | sort) b=$(dig "${new[@]}" +short "$name" "$type" | sort) if [ "$a" = "$b" ]; then printf '= %-28s %s\n' "$name" "$type" else printf '!= %-28s %s\n' "$name" "$type" diff <(echo "$a") <(echo "$b") | grep '^[<>]' | cut -c1-90 fidone
dns-compare.sh, two local DNS servers
Command
./dns-compare.sh "@127.0.0.1 -p 15353" "@127.0.0.1 -p 15354" < records.txt
Output
= example.com A= example.com AAAA= www.example.com CNAME= example.com MX= example.com TXT!= s2026._domainkey.example.com TXT< "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtcnTo5pibROMrZlF/Aw+g6me8> = _dmarc.example.com TXT
09

Change NS at the registrar and keep the old zone

You change the NS servers at the registrar, and the registry publishes them in the parent zone with its own TTL. For vulcode.com the .com server gives 172800 seconds, 48 hours, and the .pl server gives nask.pl 86400 seconds, 24 hours. That is how long resolvers may keep asking the old provider.

So the old zone stays untouched for at least that long, and any fix goes to both providers at once. You delete it only when dig NS shows the new provider from several resolvers.

dig, the .com and .pl zone servers
Command
dig +norec +noall +authority vulcode.com NS @a.gtld-servers.net
Output
vulcode.com. 172800 IN NS ns1.hosti24.pl.vulcode.com. 172800 IN NS ns2.hosti24.pl.
Command
dig +short NS pl.
Output
f-dns.pl.d-dns.pl.h-dns.pl.a-dns.pl.j-dns.pl.b-dns.pl.
Command
dig -4 +norec +noall +authority nask.pl NS @a-dns.pl
Output
nask.pl. 86400 IN NS ns.tpnet.pl.nask.pl. 86400 IN NS bilbo.nask.pl.nask.pl. 86400 IN NS eomer.nask.pl.nask.pl. 86400 IN NS kirdan.nask.pl.nask.pl. 86400 IN NS nms.cyf-kr.edu.pl.

How to check it works

We served the old and the new example.com zone on two CoreDNS 1.12.4 instances. dns-compare.sh found the DKIM key missing from the new zone, and once it was added every name on the list answered the same. We checked the redirects in nginx 1.31.6 in Docker, and the TTL and the delegation on real name servers.

We did not move a real domain between providers. The commands in steps 5 and 9 still show exactly the TTL values that decide how long a move takes.

Environment
CoreDNS 1.12.4 (old and new provider), nginx 1.31.6 in Docker, dig 9.18.39, curl 8.5.0
Result
Zones identical after adding the missing DKIM, a single 301 that keeps the path

What you should see

  • dig @new-name-server returns the same as the old one for every record on the list.
  • http://, http://www and https://www end in one 301 to the same address with the same path.
  • dig @1.1.1.1, @8.8.8.8 and @9.9.9.9 show the new address once the old TTL has passed.
  • Mail arrives, and MX and the mail TXT records are in the new zone.
dns-compare.sh and curl, local setup
Command
./dns-compare.sh "@127.0.0.1 -p 15353" "@127.0.0.1 -p 15354" < records.txt
Output
= example.com A= example.com AAAA= www.example.com CNAME= example.com MX= example.com TXT= s2026._domainkey.example.com TXT= _dmarc.example.com TXT
Command
curl -skI --resolve www.example.com:443:127.0.0.2 'https://www.example.com/pricing?plan=yearly' | grep -i -E '^(HTTP|location|strict)'
Output
HTTP/1.1 301 Moved PermanentlyLocation: https://example.com/pricing?plan=yearlyStrict-Transport-Security: max-age=31536000; includeSubDomains
  1. 1-8
    Both zones answer the same

    Every name in records.txt shows =, including s2026._domainkey, which was missing before.

  2. 9-12
    https://www to the apex

    One 301 with the full path and parameters, with the HSTS header already on the redirect.

The domain connected together with the hosting

How it looks in our project

Hand it over when the domain also carries email, when the move involves several providers at once, or when you cannot afford downtime in a store or on forms.

With VulCode web hosting we connect the domain and set up the www and non-www versions. With a website transfer we check the site on the new server under the real domain before DNS changes, and we move the email and DNS records together with the site.

Moving a website with the domain and email

Common errors

Symptom, cause and fix, with the number of the step where the error shows up.

StepSymptomCauseFix
03 Mail stopped arriving after adding a CNAME A CNAME on the apex captures the MX and TXT queries too. A and AAAA on the apex, CNAME only on www and subdomains.
04 A certificate error on https://www The certificate does not cover the www name. Issue the certificate for both names.
04 Several redirects in a row http redirects to https://www, which only then redirects to the apex. Every server block points straight to the final https address.
06 Some people see the old site for an hour The 3600 TTL was not lowered before the address change. Lower the TTL and wait out the old one before changing the address.
08 DKIM or a service verification fails after the NS change TXT records are missing from the new zone. Compare the zones with dns-compare.sh before changing NS.
09 The domain stopped resolving for some users DNSSEC: the DS record at the registrar matches the old provider's keys. Switch DNSSEC off and wait out the DS TTL before changing NS.

Frequently asked questions

The questions that come up with this setup.

How long does DNS propagation take?
A change to an A record shows up at the latest once its TTL expires in the resolvers' caches. A change of NS servers depends on the delegation TTL: in .com the NS records have 172800 seconds, 48 hours, and in .pl 86400, 24 hours. How a .pl transfer and an NS change work is covered in our post on domain transfers and propagation time.
How do I point a domain registered at home.pl, OVH or nazwa.pl to an external server?
In the registrar's DNS panel, whether that is home.pl, OVH, nazwa.pl or another, you set A and AAAA records for the apex and a CNAME for www, pointing to the server's address. The change is visible at the latest once the TTL of the previous records runs out.
How do I check whether a DNS change is already visible?
Query the domain's name server and a few public resolvers with dig, for example 1.1.1.1, 8.8.8.8 and 9.9.9.9. The name server shows what is in the zone and the resolvers show what users see; once all of them return the new address, the change is visible.
Is it better with www or without?
Both are fine. github.com chose the address without www and cloudflare.com the one with it. What matters is that there is one and the other redirects to it with a 301 that keeps the whole path.
Can I put a CNAME on the apex?
Under RFC 1034 and 2181 no, because a name with a CNAME cannot have other records, and the apex has at least SOA and NS. Some providers, Cloudflare for example, offer CNAME flattening: the server turns the target into an IP address itself and returns that instead of the CNAME.
Does a DNS change affect email?
Yes, if MX or the mail TXT records are missing from the new zone. That is why you compare MX, SPF, DKIM and DMARC together with the site's records. The mail records are covered in the SPF, DKIM and DMARC guide.
How is a domain transfer different from a DNS change?
A transfer changes the registrar you pay for the domain. A DNS change changes the servers that answer with the records. You can do one without the other, and the records do not move by themselves in either case. A .pl transfer step by step is covered in our post on moving a company domain.

Sources

The documentation and specifications we compared the commands with.

Other guides

Tools

  • Website migration checklistA website migration checklist that protects rankings: the migration type, old URL inventory, 301 redirect map, testing, change of address and monitoring.
  • Website launch checklistGenerate a website launch checklist tailored to your site: staging, redirects, robots.txt, sitemap, canonical, hreflang, structured data and Search Console.

Glossary terms

Tell us what you want to build.

A free quote comes back within an hour during working hours. You talk to the person who writes the code.