Support >
  About cybersecurity >
  Creating an alias for your domain: A guide to adding CNAME records
Creating an alias for your domain: A guide to adding CNAME records
Time : 2025-12-26 17:26:11
Edit : DNS.COM

When you want to point `www.example.com` to `example.com`, or `blog.example.com` to an address on a third-party platform (such as GitHub Pages or Shopify), you don't need to repeatedly configure complex IP addresses. This is where a very useful feature in DNS records comes in the CNAME record. It's like giving a domain name an "alias," telling visitors, "Please go directly to that person; their information is the same as my information." Whether for simplifying management, integrating services, or enabling CDN acceleration, mastering the addition of CNAME records is an essential skill for domain management.

To understand CNAME, you can first compare it to an A record. An A record is an "address record," which directly resolves a hostname (such as `example.com`) to a specific IP address (such as `192.0.2.1`), serving as the "final destination" from the domain name to the address. A CNAME record, or Canonical Name Record, resolves one hostname to another (e.g., pointing `www.example.com` to `example.com`). It acts as a "transfer station" or "alias" between domain names. When a visitor queries a record with a CNAME record, the DNS system then queries the A record corresponding to the target hostname to obtain the IP address. Its biggest advantage is centralized management: if your server's IP address changes, you only need to update the A record for the final target domain (e.g., `example.com`), and all CNAME aliases pointing to it will automatically take effect, without needing to modify them individually.

Adding a CNAME record is similar to adding other DNS records; the core steps occur in the management console of your domain registrar or DNS service provider. Although the interfaces differ, the underlying logic and the fields required are the same.

The first step is to log in and locate the DNS management zone. Log in to your service provider's backend using the account you used to purchase the domain. Find the domain you need to manage in the domain list, click "Manage," and then look for options named "DNS Resolution," "Domain Resolution," "DNS Management," or "Advanced DNS." This page is your central workbench for managing all DNS records.

The second step is to click the "Add Record" button on the DNS record list page. In the pop-up form or page, the crucial first step is to select the "Record Type." In the drop-down menu, find and select "CNAME."

The third step is to fill in the specific CNAME record information. Here you typically need to fill in two or three core fields:

1. Host Record/Name: This is the part you want to set as an "alias," which is the subdomain you want to point to elsewhere. For example, if you want `www.example.com` to point to `example.com`, then enter `www` here. If you want `shop.example.com` to point to another address, enter `shop` here. If you want to set an alias for the root domain (`example.com`) itself, some service providers may allow you to enter `@` or leave it blank, but please note an important restriction: it is generally not recommended or allowed to directly set CNAME records for the root domain, as this will cause conflicts with other necessary records (such as MX mail records).

2. Value/Target/Points to: Enter the target domain name your alias wants to point to. This is crucial; you must enter a valid and complete hostname. For example, to point to your own main website, enter `example.com.` (note that the trailing dot is sometimes required and sometimes not; follow your service provider's instructions); to point to GitHub Pages verification service, enter `yourname.github.io.`; to point to a CDN service provider, it might be `yourzone.cdnprovider.net`. Ensure the input is complete and accurate, and do not include `http://` or `https://` protocol headers.

3. TTL (Time to Live): This determines how long this record will be cached by other DNS servers. For initial setup or testing, you can set a shorter time (e.g., 300 seconds) for quick effect and modification. Once stable, you can set a longer time (e.g., 3600 seconds or 1 hour) to reduce query latency and server load.

4. Save and wait for it to take effect. After clicking the Confirm or Save button, the CNAME record will be created. The refresh and propagation of the global DNS system takes time, typically ranging from minutes to hours. You can use online global DNS lookup tools to check if a new record has taken effect.

To make the concept more concrete, let's look at two of the most common practical scenarios.

Scenario 1: Pointing the `www` subdomain to the root domain. This is the standard practice for `www.example.com` and `example.com` to access the same website. In your DNS management interface, you would fill in something like this:

Record Type: CNAME

Host Record: `www`

Record Value: `example.com` (or `example.com.`)

TTL: 1800

After saving, when someone visits `www.example.com`, the DNS system tells them, "This address is an alias for `example.com`, please look up its IP address." This achieves the goal of two domains pointing to the same content.

Scenario 2: Pointing a blog subdomain to a third-party hosting platform. For example, you might want to use `blog.example.com` to display a blog hosted on GitHub Pages. GitHub will ask you to create a CNAME record. At this point, you need to add the following in your DNS console:

Record Type: CNAME

Host Record: `blog`

Record Value: `yourusername.github.io` (Please replace with your GitHub username)

TTL: 3600

This way, all traffic accessing `blog.example.com` will be redirected to GitHub's servers.

For developers who need automated management, most major DNS providers offer APIs. Below is an example of using a Python script to call the Cloudflare API to add a CNAME record. You will need to obtain the Zone ID and API token beforehand and replace the corresponding values ​​in your code.

Python

import requests

zone_id = "Your Zone_ID"

api_token = "Your API_Token"

domain = "example.com"

cname_host = "blog" # To create blog.example.com

target = "yourusername.github.io"

url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records"

headers = {

"Authorization": f"Bearer {api_token}",

"Content-Type": "application/json"

}

data = {

"type": "CNAME",

"name": f"{cname_host}.{domain}", # Full domain name

"content": target, # Target

"ttl": 3600,

"proxied": False # Cloudflare's unique CDN proxy switch, defaulting to False, only performs DNS resolution.

}

response = requests.post(url, headers=headers, json=data)

if response.status_code == 200:

print("CNAME record added successfully!")

else:

print(f"Adding failed: {response.json()}")

When using CNAME records, there are several important technical details and pitfalls to be aware of:

First, regarding conflicts between CNAME records and other records. This is the most important principle. Under the same hostname (e.g., `www.example.com`), a CNAME record cannot exist simultaneously with any other record (such as A records, MX records, TXT records, etc.). This is because a CNAME record claims "all my information is equal to the target host's information," which contradicts the information defined by other records. The DNS standard stipulates that if a hostname has a CNAME record, then all other records should be ignored. Therefore, before adding a CNAME, ensure that there are no other important records under that hostname, or that backups and migrations have been performed.

Secondly, CNAME records generate an additional DNS lookup. Because of this extra lookup, it's theoretically slightly slower than using an A record directly. However, this delay has a negligible impact on user experience in today's network environment, and the resulting management convenience far outweighs this minor cost.

Finally, the validity of the target hostname is crucial. You must ensure that the target hostname pointed to by the CNAME record is validly configured. If the target hostname doesn't exist or cannot be resolved, your alias will also become invalid. This is like telling someone "my address is the same as my friend Zhang San's," but if Zhang San himself has no fixed address, others still won't be able to find you.

Mastering the addition of CNAME records gives you the fundamental ability to flexibly manage domain traffic. It allows subdomains to be easily delegated to different service providers, minimizing the impact of changes to the core domain, and is one of the cornerstones of building a modern, flexible website architecture.

DNS Jude
DNS Puff
DNS Luna
DNS Becky
DNS Sugar
DNS Grace
DNS Amy
DNS NOC
Title
Email Address
Type
Information
Code
Submit