Table of Contents
- Step 1: Pick a domain
- Step 2: Decide between example.com and blog.example.com
- Step 3: Buy it
- Step 4: Point DNS at GitHub
- Step 5: Verify the domain with GitHub
- Step 6: Change the site, then switch the domain
- Step 7: HTTPS
- What happens to the old address and other sites
- Forwarding the bare domain to the blog
- Locking down email on a domain that doesn’t send any
- The final DNS records
- Takeaways
- About the Author
This blog started life as a GitHub Pages project site at an address like yourname.github.io/my-blog/. That works, but it’s long, it’s tied to a GitHub username, and moving it later breaks every link. So I moved it to a domain of its own.
It turned out to involve more decisions than I expected: which domain, where to buy it, apex or subdomain, which DNS records, what to do with the registrar’s defaults, and even email, for a domain that doesn’t send any. These are my notes from the whole process, in the order I’d do it again.
The names in this post are placeholders: yourname is the GitHub user, my-blog the repository, and example.com the domain, with the blog at blog.example.com.
This is where it ends up: every address leads to the blog, which GitHub Pages serves from the repository.
Step 1: Pick a domain
Free domains mostly don’t exist anymore
The service that used to give away free domains (Freenom, with .tk, .ml and friends) stopped registrations in 2023. What’s left are free subdomains on someone else’s domain, which all work with GitHub Pages:
| Service | You get | How |
|---|---|---|
| is-a.dev | yourname.is-a.dev | A pull request to their GitHub repo. Meant for developer sites. |
| js.org | yourname.js.org | A pull request. JavaScript projects only. |
| eu.org | yourname.eu.org | Request on their site. Approval can take a long time. |
| GitHub Student Developer Pack | A real domain, free for a year | Students only. Renews at the normal price. |
The catch with all of them is that you don’t own the name. The service can shut down or remove you, and moving later costs you your links. For something meant to last, a real domain is worth about $10 a year.
Where to buy
A .com costs about $10–12 a year at registrars that don’t mark up renewals:
- Cloudflare Registrar sells at cost, but requires Cloudflare to host your DNS.
- Porkbun costs about the same, has a simpler interface and works with any DNS.
Both include WHOIS privacy for free. I’d avoid registrars that advertise a cheap first year and then renew at double the price; over five years, the “cheap” one costs twice as much. When comparing, always look at the renewal price.
The steps below describe what my registrar’s dashboard offered. Every registrar has the same pieces, sometimes under different names.
Checking if a name is free
You can ask the registry directly, without a registrar’s search page. For .com, Verisign answers RDAP queries:
curl -s -o /dev/null -w '%{http_code}\n' https://rdap.verisign.com/com/v1/domain/example.com
404 means nobody has registered it; 200 means it’s taken (drop the -o /dev/null to see who holds it and when it expires). A name that’s taken but shows a “this domain is for sale” page belongs to a reseller, and the asking price is usually in the thousands. Very occasionally a free name is sold as a “premium” domain at a higher price, so check the price in the registrar’s cart before you pay.
My first choice was taken by a reseller, so I checked a batch of variations in a loop until I found one I liked that was free. A domain based on your own name is worth considering, since it’s one you’re likely to keep for life.
Step 2: Decide between example.com and blog.example.com
Before touching DNS, decide where the blog lives, because moving it later changes every URL.
example.com(the apex) gives the shortest URL and makes the blog the main thing at the domain.blog.example.comleaves the root free for a homepage or portfolio later, and other projects can sit beside it atdemo.example.comand so on.
I chose the subdomain. Both work with GitHub Pages, but the DNS records differ (see Step 4).
Step 3: Buy it
At the registrar:
- Search for the name and add it to the cart. Check the renewal price.
- Turn auto-renew on. An expired domain can be grabbed by someone else within days.
- Leave WHOIS privacy on (it’s free) and skip every add-on: email, site builders and especially SSL certificates. GitHub Pages provides HTTPS for free.
- Create the account and pay, then turn on two-factor authentication. Your domain is only as safe as this login.
- Click the email verification link the registrar sends. Registrars must verify your contact email, and an unverified domain can be suspended after about two weeks.
Step 4: Point DNS at GitHub
A subdomain needs one record
For blog.example.com, add a single CNAME at the registrar:
| Type | Host | Answer |
|---|---|---|
| CNAME | blog | yourname.github.io |
The target is your user domain, yourname.github.io, without the repository name. GitHub works out which repository to serve from the custom domain setting in Step 6.
An apex domain needs A and AAAA records
A CNAME isn’t allowed at the bare domain, so for example.com you point at GitHub’s addresses instead:
| Type | Host | Answer |
|---|---|---|
| A | (blank) | 185.199.108.153 |
| A | (blank) | 185.199.109.153 |
| A | (blank) | 185.199.110.153 |
| A | (blank) | 185.199.111.153 |
| AAAA | (blank) | 2606:50c0:8000::153 |
| AAAA | (blank) | 2606:50c0:8001::153 |
| AAAA | (blank) | 2606:50c0:8002::153 |
| AAAA | (blank) | 2606:50c0:8003::153 |
Add CNAME www → yourname.github.io as well, and GitHub redirects www to the apex for you.
Clean up the registrar’s parking records
A new domain comes with records pointing at the registrar’s “parked” page. Mine had an ALIAS on the bare domain and a wildcard CNAME (*). The wildcard is easy to miss: it makes every name under the domain resolve, including made-up ones like anything.example.com. While trying to work out the DNS, I mistook www for a real record, but it only answered because of the wildcard.
Delete the wildcard. Don’t replace it with a wildcard pointing at GitHub either: GitHub specifically warns against wildcard records, because they let someone else claim an unused subdomain for their own Pages site.
Check the record
DNS changes at the registrar’s own nameservers are instant; other resolvers catch up within the record’s TTL (600 seconds here). You can ask both:
nslookup -type=CNAME blog.example.com 1.1.1.1
nslookup -type=CNAME blog.example.com 8.8.8.8
Both should answer canonical name = yourname.github.io.
Step 5: Verify the domain with GitHub
This step is optional but worth doing. Verifying the domain proves to GitHub that it’s yours, so nobody else can attach it, or any of its subdomains, to their own Pages site.
-
Go to your account settings (not the repository’s): Settings → Pages → Add a domain, and enter
example.com. -
GitHub shows a TXT record. Add it at the registrar:
Type Host Answer TXT _github-pages-challenge-yournamethe value GitHub shows Most registrars append the domain to the host themselves, so don’t type
.example.comon the end. Paste the value without quotes. -
Click Verify. If it fails, give DNS a few minutes and try again.
Keep this record afterwards. Removing it can make the domain unverified again. Verifying example.com covers all its subdomains, so you only do it once.
Step 6: Change the site, then switch the domain
The site config
A project site lives under /my-blog/, but on a custom domain it’s served from the root. For an Astro site, that means two settings:
// astro.config.ts
export default defineConfig({
site: "https://blog.example.com/",
base: "/", // was "/my-blog/"
});
site is used for canonical links, the sitemap and RSS, so it has to be the new address. This blog sets it through its theme config, but it ends up in the same place.
Code that builds URLs with import.meta.env.BASE_URL (or helpers built on it) picks up the new base by itself. Links typed by hand don’t: every Markdown link written as /my-blog/posts/... has to lose the prefix. A quick search for the old base finds them all:
grep -rn "/my-blog/" src
Run the full build afterwards and search the output for the old base too. The only hits left should be things that really do mention the repository, like “edit this page on GitHub” links.
Order matters
If the new config goes live while the site is still at the old address, every link on it breaks. If the custom domain is switched on while the old build is still deployed, every link breaks the other way. So I prepared the config change, waited until DNS and verification were done, and then did both together:
- Push the config change, which starts the deploy.
- Immediately set the custom domain on the repository.
The repository setting is in Settings → Pages → Custom domain, or with the GitHub CLI:
gh api -X PUT repos/yourname/my-blog/pages -f cname=blog.example.com
If your site deploys with GitHub Actions, you don’t need a CNAME file in the repository. GitHub ignores that file for Actions deployments and uses only this setting. (Sites built from a branch with GitHub’s own Jekyll build still use the file.)
Step 7: HTTPS
You don’t need to buy a certificate. GitHub requests one from Let’s Encrypt for the custom domain and renews it automatically. Mine was issued within a minute or two; GitHub says it can take up to an hour.
Once it’s there, turn on Enforce HTTPS so http:// redirects to https://:
gh api -X PUT repos/yourname/my-blog/pages -f cname=blog.example.com -F https_enforced=true
The HTTP-to-HTTPS redirect took a few more minutes to start working after that.
If the certificate never appears, check two things:
- The record points straight at GitHub. A proxy in between (Cloudflare’s orange cloud, for example) or the registrar’s “URL forwarding” on that name stops GitHub from issuing the certificate. Use plain “DNS only” records.
- No CAA record blocks it. If the domain has a CAA record, it must allow
letsencrypt.org.
You can see which certificate is being served with OpenSSL:
echo | openssl s_client -connect blog.example.com:443 -servername blog.example.com 2>/dev/null \
| openssl x509 -noout -subject -issuer
What happens to the old address and other sites
The old address redirects. After the switch, yourname.github.io/my-blog/ returns a 301 to the new domain, and deep links keep their path: yourname.github.io/my-blog/posts/hello/ goes to blog.example.com/posts/hello/. Links people saved don’t break.
Your other Pages sites don’t move. A custom domain belongs to one repository. Every other project site stays at yourname.github.io/<repo>/ unless you give it a domain of its own.
There’s one exception to watch for: don’t put a custom domain on the yourname.github.io repository, your user site. If you do, GitHub moves every project site that doesn’t have its own domain under that domain. yourname.github.io/notes/ would become <that-domain>/notes/.
Adding another project later takes two steps, and no new verification:
- At the registrar, add
CNAME demo → yourname.github.io. - In that repository, set the custom domain in Settings → Pages.
Forwarding the bare domain to the blog
With the blog on blog.example.com, the bare example.com didn’t point anywhere after I deleted the parking records. I wanted people who type the short name to land on the blog, so I used the registrar’s URL Forwarding, with one forward for the bare domain and one for www:
| Field | Value |
|---|---|
| Hostname | blank, then www for the second forward |
| Forward traffic to | https://blog.example.com |
| Type | Temporary (302) |
| Include URI path | on |
| Wildcard | off |
Some of these choices matter more than they look:
- 302, not 301. Browsers remember a permanent redirect, often for good. If I later put a homepage at
example.com, anyone who visited before would keep being sent to the blog. A temporary redirect keeps that option open and makes no difference to readers. - Wildcard off. Wildcard forwarding brings back the catch-all you just deleted.
- Include the path. A link like
example.com/posts/hello/then lands on that post instead of the home page.
The registrar adds the records forwarding needs. Mine added ALIAS records for the bare domain and www, plus _acme-challenge TXT records it uses to get an HTTPS certificate for the forwarded names. Leave those alone; it needs them again for renewals. HTTPS on the forwards worked straight away.
Put together, this is what happens when someone follows a link to the bare domain:
To use the bare domain for a real site later, remove the forwards first.
Locking down email on a domain that doesn’t send any
Many registrars also set up records for free email forwarding. Mine added two MX records and an SPF record that allows the registrar’s mail servers. I didn’t want email on this domain yet, which leaves a different problem: anyone can send mail with a From: address at your domain, and without records saying otherwise, receiving servers can’t easily tell it’s fake. A domain with your name on it is worth protecting from that.
Two TXT records say “this domain never sends email, and reject anything that claims otherwise”:
-
Delete the MX records.
-
Edit the existing SPF record rather than adding a second one; a domain must have exactly one SPF record, and two count as broken. Change its value to:
Type Host Answer TXT (blank) v=spf1 -all -
Add a DMARC record:
Type Host Answer TXT _dmarcv=DMARC1; p=reject;
Only email is affected; the blog and the forwards carry on as before. The one thing to remember is that if you later send mail as @example.com, through a newsletter service, a contact form or Gmail’s “send as”, it will be rejected until you replace these records with the ones your email provider gives you.
The final DNS records
For reference, this is the whole zone once everything was done:
| Type | Host | Answer | Purpose |
|---|---|---|---|
| CNAME | blog | yourname.github.io | The blog |
| TXT | _github-pages-challenge-yourname | (token from GitHub) | Domain verification |
| ALIAS | (blank) | (registrar’s forwarding host) | Forward example.com to the blog |
| ALIAS | www | (registrar’s forwarding host) | Forward www to the blog |
| TXT | _acme-challenge | (two tokens) | HTTPS certificate for the forwards |
| TXT | (blank) | v=spf1 -all | No email sent from this domain |
| TXT | _dmarc | v=DMARC1; p=reject; | Reject mail that pretends otherwise |
And a quick test that everything answers the way it should:
for u in https://blog.example.com/ http://blog.example.com/ \
https://example.com/ https://www.example.com/; do
echo "$u -> $(curl -s -o /dev/null -w '%{http_code} %{redirect_url}' "$u")"
done
The blog should answer 200, plain HTTP should 301 to HTTPS, and the bare domain and www should 302 to the blog.
Takeaways
- Buy a real domain for anything meant to last, at a registrar whose renewal price matches its first-year price.
- Decide between the apex and a subdomain before you start, and verify the domain with GitHub.
- Delete the registrar’s parking wildcard, and never add one that points at GitHub.
- Change the site’s base path and switch the custom domain at the same moment, so links don’t break in between.
- HTTPS is free and automatic. You don’t need to buy anything.
- A domain that doesn’t send email should say so with SPF and DMARC.
About the Author
Wei Li is a passionate software professional who builds enterprise applications with modern technologies. The guides here come from that work: each one explains a feature and shows it in working code. Read more on the About page, or find Wei Li on GitHub. |