Using Cloudflare Page Rules to Point a Root Domain to a GitBook Site

2021-01-03

GitBook only allows the usage of subdomains when pointing to a GitBook site. In the free version of GitBook, this can't be changed but an HTTP redirect via a Cloudflare page rule is a cheap, fast, and easy way to send users from a root domain to a subdomain where a GitBook site is hosted.

Example: blakejarvis.com -> www.blakejarvis.com (GitBook site)

Why Not Double CNAME?

Using a CNAME pointer to redirect blakejarvis.com to www.blakejarvis.com via DNS would make the most sense, but Cloudflare doesn't allow double CNAMEs within the Cloudflare network. Instead, a Cloudflare page rule will serve an HTTP 301 redirect to send all users to www.blakejarvis.com where the GitBook Site is hosted.

DNS Records

Add a CNAME DNS entry in the domain's DNS zone to point www.blakejarvis.com to hosting.gitbook.io. This is required by GitBook.

Add a 'dummy' DNS a record for the root site blakejarvis.com to point anywhere. In this case, Cloudflare's 1.1.1.1 resolver is used.

Cloudflare Page Rules

Create a page rule to redirect all traffic to blakejarvis.com to www.blakejarvis.com via a 301 HTTP redirect. Any 301 redirect implementation will work (e.g. PHP on Apache), but serving this rule at the Cloudflare edge will be the fastest.

Last updated