How to Use a Custom Domain for GitHub Pages

3/1/2021

The following is an opinionated “How To” on setting up a custom domain for your GitHub Pages site.

How To

1. Create and publish your CNAME file

Create and publish a CNAME file (no file extension) to the top level / root of your GitHub repo. The only contents of the CNAME file should be the domain that you want to use for your GitHub Pages site. This should be a domain that you own / control! Note, no http:// or https:// is required in the domain.

Example:

www.example.com

The real CNAME for this site:

www.seanosier.com

I recommend using a www. domain, but this is not strictly required.

2. GitHub Pages Setup

Go to your GitHub repo’s “Settings” tab / page:

Repo Settings

Scroll down to the “GitHub Pages” section, and set it up to your liking:

This site’s GitHub Pages setup:

GitHub Pages Setup

3. DNS Settings

Next, go to the DNS settings wherever your custom domain is registered.

Here’s an example of where this is on Google Domains (the domain registrar for this site):

Google Domains DNS

Update the DNS CNAME and A records as follows:

CNAME Record

Example CNAME record:

your_github_username.github.io

CNAME record for this site:

sosier.github.io

A Record

Example A record (identical to the A record for this site):

185.199.108.153
185.199.109.153
185.199.110.153
185.199.111.153

The CNAME record will handle the www. version of your domain (e.g. www.example.com). The A record will handle the domain without the www. prefix (e.g. example.com).

Final DNS for this site:

CNAME, A Record Example

(Note, Google Domains DNS likes to put the . on the end of sosier.github.io. This may or may not be the case for your domain registrar.)

4. Verify!

In the command line, use the following commands to verify the DNS update worked.

Checking the CNAME record:

dig www.example.com +nostats +nocomments +nocmd

Real example for this site:

dig www.seanosier.com +nostats +nocomments +nocmd

Expected output for this site:

;www.seanosier.com.		IN	A
www.seanosier.com.	3600	IN	CNAME	sosier.github.io.
sosier.github.io.	3599	IN	A	185.199.111.153
sosier.github.io.	3599	IN	A	185.199.110.153
sosier.github.io.	3599	IN	A	185.199.109.153
sosier.github.io.	3599	IN	A	185.199.108.153

Checking the A record:

dig example.com +noall +answer

Real example for this site:

dig seanosier.com +noall +answer

Expected output for this site:

seanosier.com.		295	IN	A	185.199.108.153
seanosier.com.		295	IN	A	185.199.109.153
seanosier.com.		295	IN	A	185.199.111.153
seanosier.com.		295	IN	A	185.199.110.153

Note, it may take a long time for the change to propagate and take effect. For this site, it took about an hour.

Once that’s working, the final test is to actually visit your site using the custom domain you set up!

5. Setting up HTTPS

Finally, I recommend enforcing HTTPS.

As we saw earlier, this is the final part of the “GitHub Pages” section of the “Settings” tab / page of your repo. To do this, just check the box and you’re done.

GitHub Pages Setup

Note, GitHub may not allow you to do this immediately. If so, you’ll need to try again later.

Tested On

Sources

  1. https://docs.github.com/en/github/working-with-github-pages/about-custom-domains-and-github-pages
  2. https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site

Like This?

for More in the Future

with Comments / Questions / Suggestions