Skip to content
Wei Li
Go back

Moving a GitHub Pages Site to Your Own Domain

Edit page
Table of Contents

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.

example.com and www.example.com redirect to blog.example.com through the registrar's forwarding, the old github.io address redirects through GitHub, and blog.example.com points to GitHub Pages with a CNAME record.

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:

ServiceYou getHow
is-a.devyourname.is-a.devA pull request to their GitHub repo. Meant for developer sites.
js.orgyourname.js.orgA pull request. JavaScript projects only.
eu.orgyourname.eu.orgRequest on their site. Approval can take a long time.
GitHub Student Developer PackA real domain, free for a yearStudents 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:

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.

I chose the subdomain. Both work with GitHub Pages, but the DNS records differ (see Step 4).

Step 3: Buy it

At the registrar:

  1. Search for the name and add it to the cart. Check the renewal price.
  2. Turn auto-renew on. An expired domain can be grabbed by someone else within days.
  3. 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.
  4. Create the account and pay, then turn on two-factor authentication. Your domain is only as safe as this login.
  5. 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:

TypeHostAnswer
CNAMEblogyourname.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:

TypeHostAnswer
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.

  1. Go to your account settings (not the repository’s): Settings → Pages → Add a domain, and enter example.com.

  2. GitHub shows a TXT record. Add it at the registrar:

    TypeHostAnswer
    TXT_github-pages-challenge-yournamethe value GitHub shows

    Most registrars append the domain to the host themselves, so don’t type .example.com on the end. Paste the value without quotes.

  3. 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:

  1. Push the config change, which starts the deploy.
  2. 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:

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:

  1. At the registrar, add CNAME demo → yourname.github.io.
  2. 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:

FieldValue
Hostnameblank, then www for the second forward
Forward traffic tohttps://blog.example.com
TypeTemporary (302)
Include URI pathon
Wildcardoff

Some of these choices matter more than they look:

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:

Sequence diagram: the browser looks up example.com, gets a 302 from the registrar's forwarding server, looks up blog.example.com, which is a CNAME for yourname.github.io, and gets the post from GitHub Pages.

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”:

  1. Delete the MX records.

  2. 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:

    TypeHostAnswer
    TXT(blank)v=spf1 -all
  3. Add a DMARC record:

    TypeHostAnswer
    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:

TypeHostAnswerPurpose
CNAMEblogyourname.github.ioThe blog
TXT_github-pages-challenge-yourname(token from GitHub)Domain verification
ALIAS(blank)(registrar’s forwarding host)Forward example.com to the blog
ALIASwww(registrar’s forwarding host)Forward www to the blog
TXT_acme-challenge(two tokens)HTTPS certificate for the forwards
TXT(blank)v=spf1 -allNo email sent from this domain
TXT_dmarcv=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

About the Author

Wei Li

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.


Edit page
Share this post:

Previous Post
Canon EOS R50 FAQ
Next Post
Introduction to OpenRouter