What is a subdomain and why do you need it?
A subdomain is part of your main domain that appears before the domain name and is separated by a dot. For example, if your domain is example.com, a subdomain could be blog.example.com or shop.example.com. Subdomains are essentially subsets of the main domain and can point to separate folders in your hosting.
Using a subdomain has many advantages. You can separate different parts of your website technically and administratively. For example, place a blog on blog.example.com and an online store on shop.example.com, without needing to purchase a new domain. Subdomains are also very useful for creating a staging environment; you can launch a new version of your site on test.example.com and, after ensuring it works correctly, move it to the main domain.
In this article, you will learn completely and practically how to create a subdomain and connect it to a specific folder in your hosting. This tutorial is useful for cPanel users as well as those using a virtual private server (VPS).
Prerequisites for creating a subdomain
Before you start, make sure you have the following:
- Access to the hosting control panel (cPanel, DirectAdmin, or Plesk).
- Access to the domain DNS settings (usually through the domain registrar's panel or the hosting's managed DNS).
- Access to the hosting file manager or SSH (to create the destination folder).
If your hosting uses shared services, all these items are usually available in your user panel. Otherwise, contact your hosting support.
Creating a subdomain in cPanel (step-by-step)
cPanel is one of the most popular hosting control panels. The following steps are for creating a subdomain in cPanel:
- Log in to cPanel and click on Subdomains under the Domains section.
- In the Subdomain field, enter the subdomain name (e.g.,
blog). - In the Document Root field, the folder path is automatically created (e.g.,
public_html/blog). You can change this path, but it is better to keep it as the default. - Click the Create button.
After this, cPanel automatically creates the destination folder (if it does not exist) and adds an A record for the subdomain. This record usually points to your server's IP address automatically.
Connecting the subdomain to a custom folder
If you want the subdomain to point to a folder other than the default one, you have two options:
- Change the Document Root during creation: In the third step, change the path to your desired folder. For example,
public_html/my-blog. - Change it after creation: After creating it, go back to the Subdomains section, click on Manage, and then edit the Document Root.
Note that the destination folder must already exist or you must have permission to create it. If you are creating the folder manually, make sure the path is accurate.
Creating a subdomain in DirectAdmin and Plesk
If you use the DirectAdmin panel, the steps are similar:
- Log in to DirectAdmin and go to the Domain Setup section.
- Select the desired domain and click on the Subdomain option.
- Enter the subdomain name and folder path, then click Add.
In Plesk, go to Websites & Domains, select the domain, and click on Add Subdomain. Then enter the name and path.
In all three panels, the core process is the same: the subdomain is linked to a specific folder in the hosting, and the necessary DNS record is created automatically.
DNS configuration for the subdomain
In most cases, the hosting panel automatically adds the DNS record. However, if the subdomain does not work, you need to manually check the A record.
To do this:
- Log in to your domain's DNS management panel (usually on the domain registrar's website).
- Add an A record.
- In the Name or Host field, enter the subdomain name (e.g.,
blog). - In the Value or Points to field, enter the IP address of your hosting server.
- Save and wait for DNS propagation (usually 1 to 24 hours).
To check if the DNS record is set correctly, use the following command in the terminal:
dig blog.example.com +short
If the output includes your server's IP, the record is working correctly.
Common mistake: forgetting the CNAME record
Some users use a CNAME record instead of an A record. However, for the main subdomain, an A record is sufficient. A CNAME record is typically used for subdomains that point to another domain (e.g., www). If you set the A record correctly, you do not need a CNAME.
Testing the subdomain and troubleshooting common issues
After creating the subdomain and configuring DNS, you need to test it. Open your browser and enter the subdomain address (e.g., http://blog.example.com). If the desired page appears, everything is working correctly.
If the subdomain does not work, check the following:
- DNS propagation: The DNS record may not have propagated across the internet yet. Wait and test again.
- Folder path: Make sure the destination folder contains an
index.phporindex.htmlfile. If the folder is empty, you will get a 404 error. - SSL: If you are using HTTPS, you need to issue an SSL certificate for the subdomain as well. In cPanel, you can do this from the SSL/TLS Status section.
- .htaccess file: If you have an
.htaccessfile in the main folder with redirect rules, it may affect the subdomain. Check the rules.
Common issue: 404 or 403 error
If you get a 404 error, it means the destination folder is empty or there is no index file. Create a simple index.html file in the folder and test again.
If you get a 403 error, it means access to the folder is restricted. Make sure the folder permissions are set to 755 and file permissions to 644. To change permissions in cPanel, use the File Manager and right-click on the folder.
Using a subdomain for a staging environment
One of the best uses of a subdomain is creating a staging environment. Suppose you want to update your WordPress site or try a new theme. Instead of risking breaking the main site, create a subdomain like test.example.com and place a copy of the site there.
To do this:
- Create a subdomain named
testand connect it to the folderpublic_html/test. - Copy the main site's files to this folder.
- Create a new database for the test site and enter its information in the
wp-config.phpfile. - Change the site URL in the new database to
http://test.example.com.
With this method, you can safely apply changes and, after ensuring everything works, move the site to the main domain.
Security and optimization tips for subdomains
Subdomains may be targets for cyber attacks. To increase security, follow these guidelines:
- Issue a separate SSL certificate for each subdomain. In cPanel, you can do this from the SSL/TLS section.
- If you are not using a subdomain, delete it to reduce the attack surface.
- For test subdomains, always protect them with a password. In cPanel, use the Directory Privacy section.
- Ensure that subdomain folders are included in your hosting backups.
From an SEO perspective, subdomains are considered separate sites. If you want the main domain's authority to pass to the subdomain, you need to use proper internal linking. You can also use Search Console to register the subdomain as a separate property.
Conclusion
Creating a subdomain and connecting it to a folder is one of the fundamental skills in hosting management that allows you to manage different parts of your website independently. By following the steps in this article, you can easily create a subdomain for a blog, online store, or staging environment.
If you use shared hosting, all the necessary tools are usually available in your user panel. If you encounter issues, first check the DNS and then the folder path. With a little care, your subdomain will be up and running in just a few minutes.
If you are looking for hosting with a simple user panel and full features for managing subdomains, ServerNet's shared hosting services are a great option. However, the steps in this tutorial can be applied to any other hosting as well.