| | |

GitHub Pages 04 📄 Custom Domains & DNS — Configuring apex domains (A/AAAA), subdomains (CNAME), and the CNAME file

⏱️ Estimated read: 9 min | |

GitHub Pages defaults to the github.io subdomain, but professional projects often require a custom domain. Using a custom domain—such as example.com or docs.project.org—provides branding authority and makes your site easier to find. Configuring this setup involves two distinct steps: updating your domain provider’s DNS records to point to GitHub’s infrastructure and adding a CNAME file to your repository to confirm ownership and routing.

This setup varies depending on whether you are using an apex domain (the root domain) or a subdomain (like www or blog). Apex domains require specific A records pointing to GitHub’s IP addresses, while subdomains are generally managed via CNAME records. Both methods require the repository to have a CNAME file, which tells GitHub to intercept requests for that domain and route them to your site.

This chapter covers the technical requirements for DNS configuration, the importance of the CNAME file, and how GitHub verifies domain ownership. We will explore the specific IP addresses required for root domain mapping and the best practices for ensuring your site handles HTTP/HTTPS correctly.

Key point: A custom domain configuration on GitHub Pages requires both external DNS record updates (pointing to GitHub’s IPs) and the inclusion of a CNAME file in the repository root to verify domain ownership and configure the routing target.


Why X exists

The branding problem. Using username.github.io identifies you as a GitHub user, but it lacks the professional, memorable identity of a dedicated domain. Businesses and serious open-source projects need their own identity to build trust and authority in the market.

The DNS routing problem. DNS (Domain Name System) is the phonebook of the internet. By default, your domain points to your registrar’s servers. GitHub Pages hosting needs a way to listen for incoming traffic intended for your domain. DNS records are the only standard mechanism to tell the internet, “Send traffic for example.com to GitHub’s servers.”

The verification problem. Because any user could theoretically set a DNS record for any domain, GitHub needs a way to ensure that you actually own the domain you are trying to use. The CNAME file acts as a gatekeeper; GitHub will not map the domain unless the repository contains a CNAME file matching that domain, proving you have write access to the repository.

The Apex/Subdomain distinction. Standards for DNS were designed long before modern web hosting, and the “Apex” (root) domain (example.com) has unique limitations compared to subdomains (www.example.com). GitHub Pages requires specific A records for Apex domains to bypass these limitations, as CNAME records are technically invalid at the root of a domain.

The trade-off. The trade-off is maintenance and propagation. DNS changes are not instantaneous; they depend on global propagation times. Managing DNS requires basic familiarity with your domain registrar’s interface, and incorrect configuration can take your entire site offline for several hours.


a. Apex Domains (A / AAAA)

An Apex domain is the root domain (e.g., example.com). DNS standards prevent you from using a CNAME record at the root of a domain because a CNAME must be the only record for that name. To point an Apex domain to GitHub Pages, you must use A records (IPv4) or AAAA records (IPv6) that map to GitHub’s specific IP addresses.

You must create four A records in your DNS provider’s dashboard, all pointing to the following IP addresses:

185.199.108.153
185.199.109.153
185.199.110.153
185.199.111.153

GitHub provides these four addresses to handle load balancing. By pointing your domain to these four IPs, you ensure that if one server is busy or down, your site traffic can be routed to another. Some DNS providers also support ALIAS or ANAME records, which act like a CNAME for the root, but A records are the industry standard for maximum compatibility.


b. Subdomains (CNAME)

Subdomains (e.g., www.example.com or blog.example.com) are much easier to manage. Since they are not the root domain, you can use a CNAME (Canonical Name) record. A CNAME record acts as an alias, telling the DNS system that www.example.com is simply a nickname for username.github.io.

Type: CNAME
Name: www
Value: username.github.io

When you use a subdomain, you are essentially pointing traffic to your GitHub-provided URL. This is superior to A records in one specific way: if GitHub ever changes its server IP addresses, your subdomain will continue to work without any action on your part, because it relies on the domain name rather than a specific static IP.


c. The CNAME file

The CNAME file is a plain text file located at the root of your repository (e.g., https://github.com/user/repo/blob/main/CNAME). It contains exactly one line: the domain name you wish to map (e.g., example.com or www.example.com).

example.com

When GitHub Pages receives an HTTP request for example.com, it checks its internal routing table. If your repository contains a CNAME file with example.com, GitHub accepts the request and serves your content. If the file is missing or contains the wrong domain, GitHub will reject the request or default to the github.io address. You can update this via the repository settings, but committing the file directly to your repository is the best practice as it ensures the configuration is version-controlled and persists if you accidentally delete the site’s deployment.


Complete Example Session

# ============================================
# PART 1: THE A RECORDS (DNS PROVIDER)
# ============================================
# In your domain registrar (GoDaddy, Namecheap, Cloudflare):
# Add 4 A records (Host: @, Value: 185.199.108.153, 109.153, 110.153, 111.153)

# ============================================
# PART 2: THE CNAME RECORD (DNS PROVIDER)
# ============================================
# Add a CNAME record for subdomains:
# Host: www
# Value: username.github.io

# ============================================
# PART 3: CREATING THE CNAME FILE
# ============================================
cd my-project-repo
echo "www.example.com" > CNAME
git add CNAME
git commit -m "Add CNAME for custom domain"
git push origin main

# ============================================
# PART 4: VERIFYING DNS PROPAGATION
# ============================================
# Use dig or nslookup to verify
dig +noall +answer www.example.com

# ============================================
# PART 5: SETTINGS CONFIGURATION
# ============================================
# Navigate to: Settings > Pages > Custom Domain
# Enter: www.example.com
# Click: Save

# ============================================
# PART 6: ENFORCE HTTPS
# ============================================
# Check the box: "Enforce HTTPS"
# (Wait a few minutes for SSL certificate generation)

# ============================================
# PART 7: TESTING THE APEX REDIRECT
# ============================================
# If you set up Apex, ensure example.com redirects to www.example.com
# (GitHub does this automatically if configured correctly)

# ============================================
# PART 8: REMOVING A DOMAIN
# ============================================
# Delete CNAME file OR remove from Settings
git rm CNAME
git commit -m "Remove CNAME"
git push

# ============================================
# PART 9: THE .NOJEKYLL IMPACT
# ============================================
# Ensure your site isn't Jekyll processing the CNAME file
# (Usually not an issue, but good to note)

# ============================================
# PART 10: TROUBLESHOOTING
# ============================================
# Check 'Custom Domain' section in Settings for "DNS Check" errors

The parts above covered the DNS provider configuration for A and CNAME records, the creation of the repository CNAME file, and the verification process.


Quick Reference

DNS Record Types

Domain TypeRecord TypeValue Example
Apex (Root)A185.199.108.153
SubdomainCNAMEuser.github.io

GitHub IP Addresses (A Records)

IP Address
185.199.108.153
185.199.109.153
185.199.110.153
185.199.111.153

Domain Configuration Steps

StepActionResponsibility
1Configure DNSDomain Registrar
2Create CNAME fileRepository
3Update GitHub SettingsGitHub Interface
4Enforce HTTPSGitHub Interface

Best Practices

✅ Do This:

# Add all four A records for Apex domains              // ✅
# Always check "Enforce HTTPS" in settings             // ✅
# Keep the CNAME file in the root of the repo          // ✅
# Use 'www' as the primary CNAME record                // ✅
# Wait 24-48 hours for full DNS propagation           // ✅

❌ Don’t Do This:

# Use only one A record (unstable)                     // ❌
# Forget the CNAME file in the repo                    // ❌
# Use A records for subdomains (use CNAME instead)     // ❌
# Use CNAME records for root domains                   // ❌
# Panic if DNS doesn't work in 5 minutes               // ❌

Common Pitfalls

PitfallWhy It HappensFix
DNS PropagationInternet cache delayWait up to 48 hours
IP mismatchOld GitHub IP usedUpdate to the 4 listed IPs
HTTPS ErrorCertificate not generatedTrigger settings update
Missing CNAME fileFile deleted from repoRe-add CNAME file to root
ConflictOther A record existsRemove conflicting A records

Real-World Examples

1. Professional Portfolio

janesmith.com pointing to janesmith.github.io using 4 A records.

2. Open Source Documentation

docs.library.org pointing to org/library using a CNAME record.

3. Personal Blog

blog.myname.dev managed via a CNAME record.

4. Project Landing Page

projectx.com using Apex records + www CNAME redirect.

5. API Documentation

api.service.io pointing to the project repository.

6. Internal Team Site

wiki.internal.com hosted on GitHub Pages (Private/Enterprise).

7. Organization Site

opensource.company.com using an A record setup.

8. Resume Website

resume.dev configured with an Apex domain.

9. Technical Workshop

2024.workshop.dev pointing to a specific branch/site.

10. CSS Framework Demo

framework.demo.io pointing to the framework’s GitHub repo.


Visual

Apex Domain Setup (The “A” Records)

┌──────────────────────────────────────────────┐
│  DNS PROVIDER                                │
│  example.com (A) ──► 185.199.108.153         │
│  example.com (A) ──► 185.199.109.153         │
│  example.com (A) ──► 185.199.110.153         │
│  example.com (A) ──► 185.199.111.153         │
└──────────────────────────────────────────────┘

Subdomain Setup (The “CNAME” Record)

┌──────────────────────────────────────────────┐
│  DNS PROVIDER                                │
│  www.example.com (CNAME) ──► user.github.io  │
└──────────────────────────────────────────────┘

The Routing Chain

┌──────────────────────────────────────────────┐
│  USER BROWSER                                │
│        │                                     │
│        ▼                                     │
│  DNS SERVER (Resolves domain)                │
│        │                                     │
│        ▼                                     │
│  GITHUB PAGES                                │
│  (Checks CNAME file in repo)                 │
│        │                                     │
│        ▼                                     │
│  SERVES SITE                                 │
└──────────────────────────────────────────────┘

Summary

ItemValue
Apex Record TypeA
Subdomain Record TypeCNAME
Repository RequirementCNAME file
HTTPS RequirementEnforce HTTPS enabled
Propagation TimeUp to 48 hours
Apex IPs4 specific GitHub IPs

Key takeaways:

  • DNS and Repo must match. You must configure your domain provider’s records to point traffic to GitHub, and your GitHub repository must confirm it accepts that traffic via the CNAME file.
  • Apex domains require A records. Because CNAMEs are invalid at the root, use the four provided A records for example.com.
  • Subdomains use CNAMEs. These are preferred when possible because they map to the github.io URL, insulating you from potential IP changes.
  • The CNAME file is mandatory. It is the verification mechanism GitHub uses to prevent unauthorized domain mapping.
  • HTTPS is automated. Once your DNS is correctly configured, GitHub will automatically generate and maintain SSL certificates, but you must enable the “Enforce HTTPS” toggle in settings.

Remember: Configuring a custom domain is a two-way handshake. The DNS record tells the world where your site is, and the CNAME file tells GitHub that your repository is authorized to serve that domain. If either side is missing or misconfigured, the connection will fail.

Was this helpful?


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!