The following is an opinionated “How To” on setting up a custom domain for your GitHub Pages site.
CNAME
fileCreate 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.
Go to your GitHub repo’s “Settings” tab / page:
Scroll down to the “GitHub Pages” section, and set it up to your liking:
To more fully control what is and is not public, I like to use a Private repo for my GitHub Pages sites.
Source: GitHub provides a few different options for where in your repo GitHub Pages will look for your site. Choose whichever option works best for you. To have more control over what is and is not public, I typically use the /docs
folder on the repo’s master
branch. (If it’s in /docs
, it’s public; if not, it’s not.)
Theme Chooser: GitHub provides a few built-in Jekyll themes that you can use. I normally manage styling / themes outside of GitHub, so I skip this section.
Custom domain: If you already created your CNAME
file as shown above, this section should be filled out for you already. If not, you would put the custom domain you want to use here, and GitHub will make the CNAME
file for you.
Enforce HTTPS: We’ll come back to this later.
This site’s GitHub Pages setup:
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):
Update the DNS CNAME
and A
records as follows:
CNAME
RecordExample CNAME
record:
your_github_username.github.io
CNAME
record for this site:
sosier.github.io
A
RecordExample 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
).
(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.)
In the command line, use the following commands to verify the DNS update worked.
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
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!
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.
Note, GitHub may not allow you to do this immediately. If so, you’ll need to try again later.