Cloudflare Transform Rules — URL Rewrite และ Header Modification แทน Page Rules

Cloudflare Transform Rules เป็นคุณสมบัติแรกใหม่ที่แทนที่ Page Rules ในการ URL Rewrite และ Header Modification ช่วยให้คุณปรับเปลี่ยน URL, Header ของ HTTP Request/Response, และลอจิกที่ซับซ้อนได้อย่างยืดหยุ่น ด้วยประสิทธิภาพที่เร็วกว่า Page Rules เก่า

ทำไมต้อง Transform Rules แทน Page Rules

ประสิทธิภาพสูงกว่า: Transform Rules ทำงานที่ Cloudflare Edge ด้วยความเร็วสูง ไม่ต้องส่งไปยัง Origin

ยืดหยุ่นมากขึ้น: สามารถเขียน Expression ที่ซับซ้อน เช่น Regular Expression, Conditional Logic ตรวจสอบหลายเงื่อนไข

ลดความสับสน: Page Rules มีการตั้งค่าที่ทับซ้อนกัน Transform Rules ชัดเจนกว่า

ประเภท Transform Rules

1. URL Rewrite: เปลี่ยน URL ของ Request ให้เป็นอื่น โดยไม่เปลี่ยน URL ที่ Browser แสดง (Internal rewrite)

Scenario: /api/* -> /v1/api/*

Expression:
(http.request.uri.path matches "/api/.*")

Action:
- Rewrite path to: /v1${http.request.uri.path}

Result:
/api/users -> /v1/api/users (ใน origin)
แต่ URL ใน Browser ยังคงเป็น /api/users

2. Header Modify: เพิ่ม, ลบ, หรือแก้ไข HTTP Header

Scenario: เพิ่ม Header ให้บอกชื่อเหตุกัน Request

Action:
- Add header: X-Forwarded-By = cloudflare
- Add header: X-Client-Country = ${cf.country}

Result:
 Origin จะเห็น Headers เหล่านี้ใน Request

การตั้งค่า Transform Rules

1. Dashboard Cloudflare > Rules > Transform Rules
2. คลิก "Create rule"
3. เลือก "Modify request URL" หรือ "Modify request header"

--- URL Rewrite ---
4. เลือก Match criteria:
   - Path contains: /old-path
   - OR: (http.request.uri.path matches "/old.*")

5. ตั้ง Action - "Rewrite to":
   - Static path: /new-path
   - Dynamic: /new${http.request.uri.path}
   - Or: /v${cf.zone_name}${http.request.uri.path}

6. Click "Save"

--- Response Header Modification ---
7. Create rule > "Modify response header"
8. Add header:
   - Header name: Cache-Control
   - Value: max-age=86400, public
9. Save

ตัวอย่างการใช้งาน

Example 1: Redirect /blog to /articles

Match:
(http.request.uri.path matches "/blog/(.*)")

Rewrite:
/articles/${regex_replace(http.request.uri.path, "/blog/(.*)", "${1}")}

Result:
/blog/hello-world -> /articles/hello-world

Example 2: Route /api/* to different origin

Match:
(http.request.uri.path matches "/api/.*")

Action:
- Set route to: api.example.com
- Rewrite path: ${http.request.uri.path}

Result:
ต้องใช้ Route Rules (ไม่ใช่ Transform Rules) แต่คิดความ
ต้องสร้าง CF-RAY ต่างกัน

Example 3: Add Security Header

Action: Modify response header

- Add: Strict-Transport-Security = max-age=31536000; includeSubDomains
- Add: X-Content-Type-Options = nosniff
- Add: X-Frame-Options = DENY
- Add: Content-Security-Policy = default-src 'self'

Result: ทั้งหมด Response จะมี Headers เหล่านี้

Example 4: Conditional Header basedบน Country

Match:
(cf.country in {"TH" "VN" "ID"})

Action:
- Add header: X-Region = Southeast Asia

Result:
Request จากประเทศเหล่านี้จะมี Header เพิ่มเติม

แตกต่างกับ Page Rules

Page Rules (เก่า) มี Limitation:

– สามารถสร้างได้ 100 rules สูงสุด
– ต้องการ Priority order (อาจสับสน)
– URL Rewrite ง่ายขึ้น แต่ Expression ไม่ยืดหยุ่น
– Performance ช้ากว่า Transform Rules

Transform Rules ดีกว่า:

– ไม่มี Limit เหมือน Page Rules (หรือถ้ามีก็มีมาก)
– Expression-based: ชัดเจน conditional
– ประสิทธิภาพ Cloudflare Edge: เร็วสูง
– สามารถรวมกับ Firewall Rules, Cache Rules ได้

สรุป

Cloudflare Transform Rules เป็นทางเลือกที่สมบูรณ์แบบแทน Page Rules เก่า ด้วยประสิทธิภาพสูง ยืดหยุ่น และง่ายต่อการเข้าใจ การ URL Rewrite และ Header Modification ทำได้สะดวก และสามารถทำซ้ำได้กับ Environment ต่างๆ รวมกับการใช้ VPS เป็น Origin ทำให้ Infrastructure ของคุณเหมาะสมที่สุด