loading...
Cover image for How to set up a custom domain on Github Pages

How to set up a custom domain on Github Pages

heymarkkop profile image Mark Kop ・2 min read

Assuming you have already bought a custom domain (example.net) and a Github Page set up (example.github.io):

Create a CNAME file

Create a file name CNAME in the project's root folder with the custom domain example.com inside.

# Outputs example.net to a file named CNAME
echo example.net > CNAME

Alt Text

You may need to configure your build command to also copy CNAME to the build folder, for example with a package.json file with React:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build && cp CNAME build/CNAME",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

Configure your domain

At your domain provider (the place you bought the domain - which I recommend porkbun.com), find the configuration for custom host/resource records and set:

Type: A
Host: @
Answer/Data/Value: 185.199.108.153
TTL: 30min (or any)

Do this for all 4 github page ip addresses: 185.199.108.153, 185.199.109.153, 185.199.110.153 and 185.199.111.153

You'll also need to add this one:

Type: CNAME
Host: www
Answer/Data/Value: example.github.io
TTL: 30min (or any)

Alt Text

Configure repository

At Github, go to the repository settings and add your custom domain in the Github Pages section.
It might take up to 24h to reflect these and/or HTTPS changes

Alt Text

References:

Posted on by:

heymarkkop profile

Mark Kop

@heymarkkop

A fullstack developer inspired by learning and sharing. (him/he)

Discussion

pic
Editor guide
 

You don't need to create the CNAME file by hand, GitHub does that automatically, when you enter the custom domain in the repository settings.

 

Cool! I was having a problem that if I didn't include CNAME on build, this setting would reset after each deploy.

 

You do have to include it on build, but when you add the custom domain from the github site it automatically creates and commits the CNAME file for you.

 

Quite handy. Thanks for sharing!

 

Thanks really useful

 

Very well explained and useful! Thanks

 

Thanks for setting up such a clear guide. Getting a custom domain working can be so frustrating because you never know if you messed something up, or if it just has not been reflected by the dns yet.