ตั้งค่า Cloudflare Page Rules / Redirect Rules สำหรับ WordPress

Cloudflare Page Rules และ Redirect Rules เป็นเครื่องมือที่มีประสิทธิภาพในการจัดการเว็บไซต์ WordPress ของคุณ ช่วยให้สามารถป้องกัน HTTPS, จัดการ Redirect, ปรับแต่ง Cache Level และความปลอดภัย เฉพาะหน้าที่ต้องการ หากคุณใช้ de.co.th Cloud VPS การตั้งค่า Rules เหล่านี้จะทำให้เว็บไซต์มีประสิทธิภาพและปลอดภัยสูงขึ้น

Page Rules vs Redirect Rules ต่างกันอย่างไร

ก่อนจะสร้าง Rules ควรเข้าใจความแตกต่างระหว่าง Page Rules และ Redirect Rules:

Page Rules (Legacy) เป็นฟีเจอร์เก่าของ Cloudflare ที่ใช้สำหรับการตั้งค่า Cache, SSL, Security เฉพาะ URL Pattern ที่กำหนด (เช่น /wp-admin, /blog)

Redirect Rules (New) เป็นฟีเจอร์ใหม่ที่ใช้สำหรับ Redirect URLs ไปยัง URLs อื่นๆ พร้อมการจัดการ Query String และ Paths

สำหรับ WordPress ควรใช้ทั้งสองอย่างร่วมกัน Page Rules สำหรับการตั้งค่า Cache และ Security, Redirect Rules สำหรับ URL Redirect

Rule ที่ 1: Force HTTPS

HTTPS เป็นสิ่งสำคัญสำหรับความปลอดภัยของ WordPress ควรให้ทุก HTTP Requests ถูก Redirect ไปยัง HTTPS โดยอัตโนมัติ

Method 1: ใช้ Cloudflare Redirect Rule

1. Security > Rules > Redirect Rules
2. Click "Create Rule"
3. Rule name: "Force HTTPS"
4. Condition:
   - Field: Scheme
   - Operator: equals
   - Value: http
5. Action: "Redirect to..."
6. Redirect to: ${scheme}://example.com${uri}
   เปลี่ยน example.com เป็นโดเมนของคุณ
7. Status code: 301 (Permanent)
8. Save

Method 2: ใช้ Cloudflare Always Use HTTPS Setting

1. Security > Settings
2. เปิด "Always Use HTTPS"
3. Save

Force HTTPS จะป้องกันการ Downgrade Attack และทำให้ SEO ดีขึ้น เพราะ Google ให้ความสำคัญกับเว็บไซต์ HTTPS มากขึ้น

Rule ที่ 2: WWW Redirect

เว็บไซต์ WordPress บางเว็บอยากให้ทุก Requests ไปยัง www หรือ non-www เพื่อหลีกเลี่ยงปัญหา Duplicate Content และการ Cookie ที่ไม่ตรงกัน

Redirect จาก non-www ไป www:

1. Security > Rules > Redirect Rules
2. Create Rule: "Redirect to WWW"
3. Condition:
   - Field: Hostname
   - Operator: does not equal
   - Value: www.example.com
4. Action: Redirect
5. Redirect to: https://www.${uri}
6. Status code: 301
7. Save

หรือการกลับกัน redirect www ไป non-www:

Condition:
   - Field: Hostname
   - Operator: starts with
   - Value: www.

Redirect to: https://example.com${uri}

Rule ที่ 3: ปรับแต่ง Cache Level

Cache Level เป็นการตั้งค่าว่าจะให้ Cloudflare บันทึก Cache ของหน้า WordPress เพียงใด ปกติ WordPress ต้องให้ Cache ที่แตกต่างกันไปตามประเภทหน้า:

Page Rules เพื่อการตั้งค่า Cache:

Rule 1: หน้าแบบ Static
URL Pattern: example.com/wp-content/*
Cache Level: Cache Everything
Browser Cache TTL: 30 days

Rule 2: หน้า WordPress Admin
URL Pattern: example.com/wp-admin/*
Cache Level: Bypass
(หมายความว่า ไม่ให้ Cache ที่ /wp-admin ให้ Requests ไปยัง Server เสมอ)

Rule 3: หน้า Homepage และ Blog
URL Pattern: example.com/
Cache Level: Aggressive
Browser Cache TTL: 4 hours

การตั้งค่า Cache ให้เหมาะสมจะช่วยให้เว็บไซต์ WordPress ของคุณแสดงผลเร็วขึ้นบน de.co.th Cloud VPS

Rule ที่ 4: ปรับแต่ง Security Level

Security Level ควรให้เหมาะกับประเภทหน้า เช่น หน้า Admin ควรให้ Security แข็งกว่า หน้า Public

Page Rule สำหรับ Admin:

URL Pattern: example.com/wp-admin/*
Security Level: High
SSL: Full (Strict)

Page Rule สำหรับ Public Pages:

URL Pattern: example.com/*
Security Level: Medium
SSL: Full

Security Level Options:
- Off: ไม่มีการป้องกัน
- Essentially Off: ป้องกันน้อย
- Low: ป้องกันบ้าง
- Medium: ป้องกันระดับกลาง (แนะนำ)
- High: ป้องกันสูง
- I'm Under Attack: ป้องกันสูงสุด ให้ CAPTCHA ทุก Request

ตัวอย่างการตั้งค่า Rules ที่สมบูรณ์

นี่คือตัวอย่างการตั้งค่า Page Rules ที่สมบูรณ์สำหรับเว็บไซต์ WordPress ทั่วไป:

Page Rule 1: Admin Pages
URL: example.com/wp-admin/*
Settings:
  - Security Level: High
  - Browser Cache TTL: 30 minutes
  - Cache Level: Bypass
  - SSL: Full (Strict)
  - Minimum TLS Version: TLSv1.2

Page Rule 2: Static Files
URL: example.com/wp-content/uploads/*
Settings:
  - Cache Level: Cache Everything
  - Browser Cache TTL: 30 days
  - Minify: CSS, JavaScript, HTML

Page Rule 3: API Endpoints
URL: example.com/wp-json/*
Settings:
  - Cache Level: Bypass
  - Security Level: Medium

Page Rule 4: Checkout/Form Pages
URL: example.com/checkout/*
Settings:
  - Cache Level: Bypass
  - Security Level: High
  - SSL: Full (Strict)

ใช้ Redirect Rules สำหรับ URL Rewrite

Redirect Rules สามารถใช้ในการเปลี่ยนแปลง URLs เช่น ย้ายหน้า เปลี่ยน URL Structure หรือลบ Query Parameters:

Redirect Rule 1: ย้ายหน้า Blog
Condition: URI Path equals /old-blog/*
Action: Redirect to https://example.com/new-blog${path}
Status: 301 (Permanent)

Redirect Rule 2: ลบ Query Parameters ที่ไม่จำเป็น
Condition: Query String contains "utm_source"
Action: Redirect to ${scheme}://${host}${path}
Status: 301

Redirect Rule 3: ป้องกัน Direct Access ไปยัง /wp-json
Condition: URI Path equals /wp-json/*
And: Country does not equal TH
Action: Block (หรือ Redirect ไปหน้า)

ประเมินผล Rules

หลังจากตั้งค่า Rules แล้ว ควรตรวจสอบว่า Rules ทำงานได้ถูกต้อง:

1. ไปที่ Analytics > Events
2. ดูจำนวน Requests ที่ถูก Cache, Redirect, Block
3. ตรวจสอบ Status Codes (200, 301, 403 เป็นต้น)
4. ทดสอบ Website Speed ด้วย GTmetrix หรือ PageSpeed Insights
5. ตรวจสอบว่า WordPress Functions ยังทำงานปกติหรือไม่

คำแนะนำสำหรับ de.co.th Cloud VPS

เมื่อใช้ de.co.th Cloud VPS ร่วมกับ Cloudflare ควรสังเกตบ้าง:

– ตั้งค่า Origin Server IP ของ Cloud VPS ให้ถูกต้องใน Cloudflare
– เปิด CloudFlare IP Whitelist บน Firewall ของ VPS
– ตั้งค่า SSL/TLS ให้เป็น “Full (Strict)” เพื่อให้ Server และ Cloudflare สื่อสารอย่างปลอดภัย
– ตรวจสอบ Server Logs เพื่อดูประสิทธิภาพจริงของ VPS

สรุป

การตั้งค่า Cloudflare Page Rules และ Redirect Rules ให้เหมาะสมจะช่วยให้เว็บไซต์ WordPress ของคุณมีความเร็วสูง ปลอดภัยมากขึ้น และจัดการ URLs ได้อย่างมีระเบียบ

ร่วมกับ de.co.th Cloud VPS ที่มีความสมดุลระหว่างราคา และประสิทธิภาพ คุณจะได้รับ WordPress Server ที่ดีและปลอดภัยแน่นอน