สร้าง Subdomain บน Hosting ตั้งค่าอย่างไร

สร้าง Subdomain สำหรับ Staging, App, Blog หรือ API ผ่าน cPanel ตั้งค่า SSL และ Redirect ได้ทันที

สร้าง Subdomain บน Hosting ตั้งค่าผ่าน cPanel

Subdomain คืออะไร และใช้ทำอะไร?

Subdomain คือส่วนที่อยู่หน้าชื่อ Domain หลัก คั่นด้วยจุด เช่น blog.example.com, shop.example.com หรือ api.example.com โดย blog, shop และ api คือ Subdomain ของ example.com

การใช้งาน Subdomain ที่พบบ่อย:

Subdomain ต่างจาก Addon Domain ตรงที่ไม่ต้องจดทะเบียนใหม่ ใช้โดเมนเดิมได้เลย และ cPanel จัดการ DNS ให้อัตโนมัติ

สร้าง Subdomain ใน cPanel

  1. ล็อกอิน cPanel แล้วไปที่หมวด Domains
  2. คลิก Subdomains
  3. ในช่อง Subdomain พิมพ์ชื่อที่ต้องการ เช่น staging
  4. เลือก Domain ที่ต้องการสร้าง Subdomain
  5. Document Root จะสร้างอัตโนมัติเป็น public_html/staging หรือปรับเองได้
  6. คลิก Create

cPanel จะสร้าง Subdomain และเพิ่ม DNS Record (CNAME หรือ A Record) อัตโนมัติ รวมถึงสร้าง Folder ที่กำหนดด้วย

Tip: ชื่อ Subdomain ควรใช้ตัวอักษรพิมพ์เล็กและขีดกลาง (hyphen) เท่านั้น ห้ามใช้ Underscore หรืออักขระพิเศษ

ชี้ Subdomain ไปยัง Directory

โดย Default cPanel จะสร้าง Folder ใหม่ใน public_html/ สำหรับ Subdomain แต่คุณสามารถเปลี่ยนให้ชี้ไปยัง Directory ที่มีอยู่แล้วได้:

  1. ในหน้า Subdomains คลิก Document Root หรือไอคอน Edit ข้าง Subdomain
  2. เปลี่ยน Path เป็น Directory ที่ต้องการ เช่น public_html/myapp/public
  3. คลิก Save

ถ้าต้องการ Redirect Subdomain ไปที่ URL อื่นแทน ให้สร้างไฟล์ .htaccess ใน Folder ของ Subdomain:

RewriteEngine On
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

ตั้ง SSL สำหรับ Subdomain

Subdomain ต้องการ SSL Certificate ของตัวเอง วิธีที่ง่ายที่สุดคือใช้ Let's Encrypt ผ่าน cPanel:

  1. ไปที่ cPanel > SSL/TLS > Let's Encrypt SSL (หรือ AutoSSL)
  2. เลือก Subdomain ที่ต้องการออก Certificate
  3. คลิก Issue หรือ Run AutoSSL
  4. รอ 1–2 นาที Certificate จะถูก Install อัตโนมัติ

ถ้า Hosting ใช้ Cloudflare ก็สามารถใช้ Cloudflare SSL ฟรีสำหรับ Subdomain ได้โดยเพิ่ม CNAME Record ใน Cloudflare Dashboard

Redirect Subdomain

สามารถ Redirect Subdomain ได้หลายวิธีตามความต้องการ:

# Redirect staging ไป main domain (301 Permanent)
RewriteEngine On
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

# Redirect www ไป non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

# Redirect HTTP ไป HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

ตรวจสอบว่า Subdomain ทำงานถูกต้อง

# ตรวจสอบ DNS ของ Subdomain
dig staging.example.com +short
nslookup staging.example.com

# ตรวจสอบ A Record
dig staging.example.com A

# ทดสอบ HTTP Response
curl -sI http://staging.example.com

# ตรวจสอบ SSL Certificate
curl -sI https://staging.example.com | grep -E "HTTP|strict"

# ตรวจสอบว่า Subdomain ชี้ถูก IP
curl -sI https://staging.example.com | grep cf-ray
Tip: ถ้า dig ไม่แสดงผลหลังสร้าง Subdomain รอ 5–10 นาทีก่อน เพราะ DNS Record ต้องการเวลา Propagate เล็กน้อยแม้อยู่ใน Nameserver เดียวกัน

แก้ปัญหาที่พบบ่อย

Subdomain ไม่ Resolve — แสดง "This site can't be reached"

ตรวจสอบว่า DNS Record ถูกสร้างในระบบ cPanel และยัง Propagate อยู่หรือไม่ ใช้ dig subdomain.yourdomain.com ถ้าไม่มีผลลัพธ์ ให้เข้า cPanel > Zone Editor ตรวจสอบว่ามี A Record หรือ CNAME ของ Subdomain หรือไม่

Certificate Error บน Subdomain

เกิดเมื่อ Subdomain ไม่มี SSL Certificate หรือ Certificate หมดอายุ ให้รัน AutoSSL ใน cPanel อีกครั้ง หรือ Issue Certificate ใหม่ผ่าน Let's Encrypt ถ้า AutoSSL ไม่ทำงาน ตรวจสอบว่า cPanel สามารถ Verify Subdomain ผ่าน HTTP-01 Challenge ได้ (ไม่มี .htaccess บล็อก /.well-known/)

Subdomain แสดง Index ผิด Folder

Document Root ตั้งค่าผิด เข้าไปแก้ใน cPanel > Subdomains คลิก Edit ข้าง Subdomain แล้วแก้ไข Document Root ให้ถูกต้อง

Subdomain ถูกลบจาก cPanel แต่ DNS ยังชี้อยู่

ต้องลบ DNS Record ออกด้วยตนเองที่ cPanel > Zone Editor ลบ A Record หรือ CNAME ที่เกี่ยวข้องกับ Subdomain นั้นออก

คำถามที่พบบ่อย

Subdomain กับ Subdirectory อันไหนดีกว่าสำหรับ Blog?

ในแง่ SEO ปัจจุบัน Google ปฏิบัติต่อ Subdomain และ Subdirectory ค่อนข้างใกล้เคียงกัน แต่ Subdirectory เช่น example.com/blog มักได้ประโยชน์จาก Domain Authority ของ Main Domain มากกว่า ส่วน Subdomain เช่น blog.example.com เหมาะเมื่อต้องการแยก Platform เช่น WordPress แยกต่างหาก

จำนวน Subdomain ที่สร้างได้มีข้อจำกัดไหม?

cPanel เองไม่ได้จำกัด แต่ Hosting Provider บางรายกำหนด Limit ไว้ในแผนราคาถูก สำหรับแผนมาตรฐานขึ้นไปมักไม่มีข้อจำกัด นอกจากนี้ SSL Certificate แบบ Wildcard ครอบคลุม Subdomain ทุกตัวในระดับเดียวด้วยใบเดียว

Subdomain ต้องเสียค่าจดทะเบียนเพิ่มไหม?

ไม่ต้องเลย Subdomain ใช้ชื่อโดเมนที่มีอยู่แล้วและสร้างได้ฟรีไม่จำกัดจำนวน ต่างจาก Addon Domain ที่ต้องซื้อโดเมนใหม่เพิ่ม