cPanel
Redirect Subdomain ใน cPanel คืออะไร?
การ Redirect Subdomain คือการกำหนดให้ subdomain เช่น shop.yourdomain.com หรือ blog.yourdomain.com พา ผู้เข้าชมไปยัง URL อื่นโดยอัตโนมัติ ไม่ว่าจะเป็นหน้าอื่นภายในเว็บไซต์เดียวกัน หรือเว็บไซต์ภายนอกทั้งหมด เทคนิคนี้มีประโยชน์มากในสถานการณ์ต่าง ๆ เช่น ย้าย platform ร้านค้า, รวม subdomain หลายตัวเข้าด้วยกัน, หรือสร้าง vanity URL สำหรับแคมเปญการตลาด
ใน cPanel มีวิธีตั้งค่า Redirect Subdomain อยู่ 2 ช่องทางหลัก ได้แก่ Redirects Tool (วิธีที่ง่ายและแนะนำสำหรับผู้ใช้ทั่วไป) และ Zone Editor (สำหรับผู้ที่ต้องการควบคุม DNS record เองแบบละเอียด) บทความนี้จะอธิบายทั้งสองวิธีพร้อมตัวอย่างจริงที่นำไปใช้ได้ทันที
วิธีที่ 1: ใช้ Redirects Tool ใน cPanel
Redirects Tool เป็นเครื่องมือที่ง่ายที่สุดสำหรับการตั้งค่า Redirect ใน cPanel โดยไม่ต้องแตะไฟล์ .htaccess ด้วยตนเอง cPanel จะสร้างกฎ redirect ให้อัตโนมัติ
- เข้าสู่ระบบ cPanel ของคุณ (โดยปกติที่
yourdomain.com:2083) - ในหมวด Domains ให้คลิก Redirects
- เลือกประเภท Redirect ในช่อง Type:
- Permanent (301) — ใช้เมื่อย้ายถาวร เหมาะสำหรับ SEO เพราะ Google จะส่ง link juice ตาม
- Temporary (302) — ใช้เมื่อ redirect ชั่วคราว เช่น กำลังซ่อมบำรุงเว็บ
- ในช่อง https?://(www.)? เลือกโดเมนหลักของคุณจาก dropdown
- ในช่อง / (ด้านขวา) ให้พิมพ์ subdomain ที่ต้องการ redirect เช่น
shop(จะได้shop.yourdomain.com) - ในช่อง Redirects To ใส่ URL ปลายทาง เช่น
https://store.yourdomain.comหรือhttps://external-shop.com - เลือก www Redirection:
- Only redirect with www — redirect เฉพาะ www.subdomain
- Redirect with or without www — redirect ทั้ง subdomain และ www.subdomain
- Do Not Redirect www — redirect เฉพาะ subdomain ไม่มี www
- ติ๊กช่อง Wild Card Redirect หากต้องการให้ทุก path ใต้ subdomain นั้น redirect ไปด้วย (เช่น
shop.yourdomain.com/product/abcก็ไปด้วย) - คลิก Add เพื่อบันทึก
shop.yourdomain.com เปล่า ๆ เท่านั้น path อื่น ๆ จะ 404วิธีที่ 2: ใช้ Zone Editor เพิ่ม CNAME แล้ว Redirect ด้วย .htaccess
กรณีที่ subdomain ยังไม่มี DNS record อยู่เลย และคุณต้องการ redirect ไปยังโดเมนภายนอก วิธีที่ได้ผลชัวร์คือสร้าง CNAME record ก่อน แล้วค่อยใส่ redirect ในไฟล์ .htaccess
ขั้นตอนที่ 1: สร้าง Subdomain ใน cPanel
- ไปที่ Domains > Subdomains
- พิมพ์ชื่อ subdomain (เช่น
shop) เลือก domain หลัก แล้วคลิก Create - cPanel จะสร้างโฟลเดอร์ document root ให้อัตโนมัติ เช่น
public_html/shop
ขั้นตอนที่ 2: แก้ไข .htaccess ของ Subdomain
เปิด File Manager ใน cPanel ไปที่โฟลเดอร์ของ subdomain (เช่น public_html/shop) แล้วสร้างหรือแก้ไขไฟล์ .htaccess ดังนี้:
# .htaccess สำหรับ shop.yourdomain.com
# Redirect ทุก request ไปยัง URL ปลายทาง
Options -Indexes
RewriteEngine On
# Redirect แบบ Permanent (301) ไปยัง URL ภายนอก
RewriteRule ^(.*)$ https://store.example.com/$1 [R=301,L]
# หากต้องการ redirect ไปหน้าเดียวโดยไม่เอา path ตาม ให้ใช้บรรทัดนี้แทน:
# RewriteRule ^(.*)$ https://store.example.com/ [R=301,L]
[R=301,L] สำหรับ permanent redirect (SEO-friendly) และ [R=302,L] สำหรับ temporary redirect ธง L หมายถึง "Last rule" คือหยุดประมวลผล rule อื่นหลังจากนี้ตั้งค่า Redirect ด้วย Zone Editor (DNS CNAME)
กรณีที่ต้องการให้ subdomain ชี้ไปยังโดเมนอื่น (ไม่ใช่แค่ redirect แต่เป็นการ alias DNS) ให้ใช้ Zone Editor ใน cPanel เพิ่ม CNAME record:
- ไปที่ Domains > Zone Editor แล้วคลิก Manage หน้าโดเมนที่ต้องการ
- คลิก Add Record แล้วเลือกประเภท CNAME
- ในช่อง Name ใส่ชื่อ subdomain เช่น
shop.yourdomain.com.(ต้องมีจุดท้าย) - ในช่อง Record ใส่โดเมนปลายทาง เช่น
external-store.com. - ตั้ง TTL ไว้ที่
14400(4 ชั่วโมง) หรือ3600(1 ชั่วโมง) หากคาดว่าจะเปลี่ยนบ่อย - คลิก Add Record เพื่อบันทึก
; ตัวอย่าง DNS Zone record ที่ถูกสร้างโดย Zone Editor
; CNAME record สำหรับ subdomain
shop.yourdomain.com. 14400 IN CNAME external-store.com.
; A record ทั่วไปสำหรับ subdomain ที่ชี้ไป IP เดียวกับ server
blog.yourdomain.com. 14400 IN A 203.0.113.50
yourdomain.com เป็น CNAME ไม่ได้) แต่ใช้กับ subdomain ได้ทุกกรณีเปรียบเทียบ: Redirects Tool vs Zone Editor vs .htaccess
- Redirects Tool — ง่ายที่สุด ไม่ต้องมีความรู้ด้าน server; เหมาะสำหรับ redirect ปกติในโฮสติ้งเดียวกัน
- CNAME ใน Zone Editor — ใช้เมื่อต้องการให้ subdomain ชี้ไปยังเซิร์ฟเวอร์อื่น เช่น Shopify, HubSpot, หรือ CDN
- .htaccess — ยืดหยุ่นสูงสุด รองรับ pattern matching, conditional redirect, header manipulation; ต้องมีความรู้ Apache mod_rewrite
ตัวอย่างการใช้งานจริง
กรณีที่ 1: Redirect shop.yourdomain.com ไป Facebook Shop
ใน Redirects Tool เลือก Permanent (301), ใส่ shop เป็น subdomain, URL ปลายทาง: https://www.facebook.com/yourpage/shop แล้วกด Add
กรณีที่ 2: Redirect blog.yourdomain.com ไปยัง /blog/ ในโดเมนหลัก
# .htaccess ใน public_html/blog/
RewriteEngine On
RewriteRule ^(.*)$ https://yourdomain.com/blog/$1 [R=301,L]
กรณีที่ 3: Redirect subdomain เก่าไปยัง subdomain ใหม่ พร้อมเก็บ path
# .htaccess ใน public_html/old-shop/
RewriteEngine On
# เก็บ path เดิมไปด้วย เช่น old-shop.domain.com/product/xyz -> new-shop.domain.com/product/xyz
RewriteRule ^(.*)$ https://new-shop.yourdomain.com/$1 [R=301,L]
ตรวจสอบว่า Redirect Subdomain ทำงานถูกต้อง
# ทดสอบ redirect ด้วย curl (แสดง HTTP status code และ Location header)
curl -I https://shop.yourdomain.com
# ทดสอบแบบตามทุก redirect จนถึงปลายทาง
curl -IL https://shop.yourdomain.com
# ตรวจ DNS propagation ของ subdomain
dig shop.yourdomain.com CNAME +short
# ตรวจ A record หรือ CNAME ปัจจุบันที่ subdomain ชี้อยู่
nslookup shop.yourdomain.com
# ตรวจว่า .htaccess มีผล (ควรเห็น 301/302 ใน header)
curl -v https://shop.yourdomain.com 2>&1 | grep -E "HTTP|Location"
แก้ปัญหาที่พบบ่อย
Error: "Redirect Loop Detected" หรือ ERR_TOO_MANY_REDIRECTS
เกิดขึ้นเมื่อ subdomain redirect ไปยัง URL ที่ redirect กลับมาหา subdomain อีกรอบ สาเหตุที่พบบ่อยคือตั้งค่า redirect ซ้ำทั้งใน Redirects Tool และใน .htaccess พร้อมกัน
วิธีแก้: เข้าไปที่ Redirects Tool ลบ redirect ที่สร้างไว้ออกก่อน แล้วเหลือไว้เพียงวิธีเดียว (Tool หรือ .htaccess) อย่าใช้ทั้งสองพร้อมกัน
Error: Subdomain แสดง "This site can't be reached" หลัง redirect
มักเกิดจาก DNS ยังไม่ propagate หรือ subdomain ไม่มี DNS record อยู่เลย แม้ตั้ง redirect ใน .htaccess แล้ว browser ก็หา IP ไม่เจอก่อนจะถึง server
วิธีแก้: ตรวจสอบให้แน่ใจว่า subdomain มี A record หรือ CNAME ใน Zone Editor ก่อน แล้วค่อยใส่ redirect rule
Error: Redirect ทำงานแต่ไม่เก็บ HTTPS (ลง http://)
เกิดจาก .htaccess redirect URL ปลายทางไม่ได้ใส่ https:// นำหน้า หรือเซิร์ฟเวอร์ไม่มี SSL certificate สำหรับ subdomain
วิธีแก้: ตรวจสอบ URL ใน RewriteRule ให้ขึ้นต้นด้วย https:// และติดตั้ง SSL certificate สำหรับ subdomain ผ่าน cPanel > SSL/TLS > Let's Encrypt
Error: Wild Card Redirect ไม่ทำงานสำหรับ path ย่อย
ถ้าใช้ Redirects Tool แต่ลืมติ๊ก Wild Card Redirect จะ redirect แค่ root ของ subdomain เท่านั้น path อื่น เช่น shop.yourdomain.com/product/abc จะ 404
วิธีแก้: กลับไปที่ Redirects Tool ลบ redirect เดิมออก แล้วสร้างใหม่พร้อมติ๊ก Wild Card Redirect
Error: .htaccess ไม่มีผล ทั้งที่ไฟล์ถูกต้อง
เกิดจาก mod_rewrite ไม่ได้เปิดใช้งาน หรือ AllowOverride ใน Apache config ถูกตั้งเป็น None
วิธีแก้: ติดต่อ Hosting Support เพื่อขอให้เปิด AllowOverride All สำหรับ directory ของคุณ (โฮสติ้งส่วนใหญ่เปิดไว้ให้แล้วโดย default)
คำถามที่พบบ่อย
Q: Redirect แบบ 301 กับ 302 ต่างกันอย่างไร และควรใช้แบบไหน?
A: 301 Permanent Redirect บอก search engine ว่าหน้านี้ย้ายไปถาวร Google จะส่ง link juice และ ranking ไปยัง URL ใหม่ภายใน 2-4 สัปดาห์ เหมาะสำหรับการย้าย subdomain หรือ restructure URL ถาวร 302 Temporary Redirect บอกว่าย้ายชั่วคราว search engine จะยังคง index URL เดิมไว้ เหมาะสำหรับ maintenance หรือ A/B testing โดยทั่วไปแนะนำให้ใช้ 301 เสมอ ยกเว้นแน่ใจว่า redirect เป็นการชั่วคราวจริง ๆ
Q: Redirect Subdomain จะกระทบ SEO ของเว็บหลักไหม?
A: การ redirect subdomain ด้วย 301 ที่ถูกต้องจะไม่กระทบ SEO ของโดเมนหลัก เพราะ subdomain ถือเป็นคนละ entity กัน อย่างไรก็ตาม หากเดิม subdomain นั้นมี content และ traffic เยอะ ควรส่ง sitemap ใหม่ให้ Google Search Console รับทราบ และรอ Google recrawl ประมาณ 2-4 สัปดาห์
Q: สามารถ Redirect Subdomain หลาย ๆ ตัวพร้อมกันได้ไหม?
A: ได้ สามารถเพิ่ม redirect ได้ไม่จำกัดจำนวนใน Redirects Tool ของ cPanel แต่ละ subdomain ตั้งค่าแยกกัน หรือถ้าต้องการ redirect subdomain จำนวนมากในรูปแบบเดียวกัน ก็สามารถใช้ .htaccess ที่ root domain ด้วย RewriteCond ตรวจ HTTP_HOST แทนได้ ซึ่งจัดการได้หลาย subdomain ในไฟล์เดียว