乐闻世界logo
搜索文章和话题

How do I set up GitHub Pages to redirect DNS requests from a subdomain ( e . G . Www) to the top-level domain ( TLD , Apex record)?

1个答案

1

1. Setting up the GitHub Pages Repository

First, ensure you have a GitHub repository for hosting your website files. In the repository settings, locate the 'Pages' section and select a branch as your deployment source.

2. Configuring the Top-Level Domain

In the repository's Pages settings, add a custom domain. Here, enter your top-level domain, such as example.com.

3. Updating DNS Records

Next, log in to your domain registrar's management interface to configure DNS settings.

  • Adding A Records for the Top-Level Domain GitHub provides specific IP addresses for A records, which must be added to the DNS settings for the top-level domain. For example:

    shell
    185.199.108.153 185.199.109.153 185.199.110.153 185.199.111.153
  • Adding CNAME Records for the www Subdomain For subdomains like www.example.com, add a CNAME record pointing to example.com.

4. Redirect Settings

While DNS records handle resolution from www to the top-level domain, HTTP-level redirects may be necessary to ensure users accessing www.example.com are redirected to example.com. GitHub Pages automatically manages www-to-non-www redirects for the top-level domain, so once DNS is correctly configured, this step typically requires no additional user action.

5. Testing the Configuration

Finally, verify both www.example.com and example.com are accessible, with the www version correctly redirecting to the top-level domain. Use tools like curl to test HTTP headers:

bash
curl -I www.example.com

This should display a 301 redirect to example.com.

Example

Suppose I have a project with the domain example.com. I followed the above steps to set up GitHub Pages and DNS. Initially, I configured only the top-level domain in GitHub Pages and added A records for it in the DNS provider. Later, I discovered that accessing www.example.com did not redirect to example.com, so I added a CNAME record for www pointing to example.com. After a few hours, DNS propagated, and everything worked smoothly, with www.example.com successfully redirecting to example.com.

This method ensures that regardless of how users input the domain, it is unified to a single address, thereby avoiding content duplication and fragmented SEO weight issues.

2024年8月16日 00:24 回复

你的答案