DEV Community

Dan Han
Dan Han

Posted on • Originally published at programmerdays.com on

Custom Domain For Github Pages

I recently set up github pages to use as my blogging platform. And I have had no major complaints so far. I already added lots of bells and whistles: commenting, analytics, rss feed etc… Today, I’ve set up custom domain for my github pages.

I already had a domain with Google Domains, so I just had to point it to my github pages site. To get it working, I followed this medium post and the documentation from github. Now that it’s all done, going to any of these urls, programmerdays.com, wwww.programmerdays.com, dannyhan12.github.io should all take you to “programmerdays.com” which shows my github pages site.

The process involves just 3 steps, but I found it a little confusing at first. Here’s some notes that explains what is going on.

Step 1

First, update Google Domains or your DNS provider to send your apex domain, such as "<my-awesome-custom-domain>.com" to the IP addresses owned by github. This is pretty straightforward. You need to tell the "internet" where to find the server that will process your requests.

Step 2

Next, update the settings of your github project to use your custom domain. Now, when Github servers get requests for “my-awesome-custom-domain>.com", they'll be able to properly map it to your github pages, "<username>.github.io". Conversely, if you make a request to the original github pages, the url is auto-magically updated to your custom domain.

I used this curl command to review requests

curl -v -L -D - https://programmerdays.com -o /dev/null 2>&1 | egrep '>|<|\*' | less , 

Enter fullscreen mode Exit fullscreen mode

and I could see that the request is sent to one of the github IP addresses and that it immediately returns with a successful code, 200. If I run the same curl command with the url of my github pages, “<username>.github.io", I get a 301 redirect rcode. Then another request is sent to my custom domain.

Step 3

Finally, update Google Domains again. Add a CNAME for the subdomain, “www”, to your "<username>.github.io." page. Now, requests going to “www.<my-awesome-custom-domain>.com" will also get your github pages page. The github docs mention that the proper redirects work between a subdomain and an apex domain.

Conclusion

Setting up a custom domain was pretty straightforward. I got it working without too much trouble, or even understanding. I then took a little time to learn about DNS records and HTTP redirects. Getting something working and learning something is a nice double win for me.

Top comments (0)