Sub topic 9
Adding a custom domain
This chapter assumes you have a working Pages site published from a branch or a workflow, and that you own a domain name at a registrar. You should know how to edit the site's repository settings. DNS record details and HTTPS get their own chapters, so this one covers only the GitHub side of the setup.
What a custom domain changes
By default a site lives at USERNAME.github.io or USERNAME.github.io/REPO. A custom domain such as example.com or www.example.com replaces that address. GitHub then serves the same files under the new name. The old github.io address redirects to the custom domain.
A custom domain also removes the project path. A project site that was at USERNAME.github.io/my-repo/ is served from the root of example.com. Asset links that you wrote with the /my-repo/ prefix will break after the change.
Choosing the domain form
You can use one of two forms:
- An apex domain is the bare name, such as
example.com. - A subdomain has a prefix, such as
www.example.comordocs.example.com.
If you configure an apex domain and also create a www record, GitHub redirects between the two. It does this automatically, so you only enter one of them in the settings.
Setting the domain in the repository
- Open the repository on GitHub and go to Settings, then Pages.
- In the Custom domain field, type your domain, for example
example.com. - Select Save.
GitHub starts a DNS check. It stays in a failing state until your registrar serves the right records, which you set up in the next chapter.
The CNAME file and publishing sources
What happens next depends on your publishing source.
With branch publishing, saving the domain makes GitHub commit a file named CNAME to the published folder of that branch. The file contains only the domain name, on one line:
example.comIf you edit the file by hand, keep it to one line with no https:// prefix and no path. If the file is removed or changed, the domain setting changes with it. Pull before you push, because the commit made by GitHub exists only on the remote branch. A later push from a stale local copy can fail, or it can delete the file.
With a GitHub Actions workflow, a CNAME file in the artifact is ignored. The domain is stored only in the Pages settings. Setting it there is enough, and you do not need to add the file.
Verifying the domain
A domain that points at GitHub Pages without being verified can be claimed by someone else. If a repository is deleted, or the Pages site is unpublished while the DNS records stay in place, another account could publish content on your name. This is called a domain takeover. To prevent it, verify the domain for your account or organization under Settings, then Pages. GitHub gives you a TXT record to add at your registrar. You can also remove the DNS records when you stop using the site.
Checking your work
Once the records exist, you can confirm that your domain resolves with dig:
dig +short example.com A
dig +short www.example.com CNAMEThe first command should list GitHub's addresses. The second should print USERNAME.github.io.. DNS changes can take from a few minutes up to a day to spread. A failing check right after you save is normal.
Common errors
| Symptom | Likely cause |
|---|---|
| Settings show "DNS check unsuccessful" | Records missing, mistyped, or not yet propagated |
| Domain resets to blank after a push | The `CNAME` file was deleted or overwritten on a branch-published site |
| Page loads but styles are missing | Asset links still use the old `/REPO/` prefix |
| Domain reset after switching to Actions | The domain was only in a `CNAME` file, not in the settings |
Exercise
A site is published with a GitHub Actions workflow. You add a CNAME file containing example.com to the site folder, but the custom domain field in Settings stays empty. Why, and what should you do?
Show answer
With Actions publishing, GitHub ignores a CNAME file in the uploaded artifact. Enter example.com in the Custom domain field under Settings, then Pages, and save it. The CNAME file is optional and can be removed.