Transform Rules
Transform Rules give you precise control over the headers sent in HTTP requests and responses. With this feature, which works similarly to response transformers, you can rewrite, add, or modify headers based on your service's needs.
For example, you can set the value of the Access-Control-Allow-Origin header dynamically based on the Origin of the incoming request, without any change on the origin server.
How Transform Rules Work
Transform Rules work based on if-then conditional logic. You can define conditions so the desired header changes are applied only when a request matches those conditions.
- If: Incoming requests match the specified conditions (such as a specific path, HTTP method type, and so on).
- Then: The requested changes are applied to the specified headers.
Currently, you can set the following headers through Transform Rules:
- Access-Control-Allow-Origin
- Access-Control-Expose-Headers
- Access-Control-Max-Age
- Access-Control-Allow-Credentials
- Access-Control-Allow-Methods
- Access-Control-Allow-Headers
For each of the headers above, the replacement value can be one of the following:
- Equal to an incoming request header (Dynamic): In this mode, the header value is extracted from the user's request. This value can be read from the requester's Host or Origin header.
- Custom value (Static): You can set a fixed text value of your choice for the header (for example, a specific domain, or values such as true for Credentials).
Setting Up Transform Rules
In the CDN section of the panel, select your domain, and from the CDN menu, choose Transform Rules. Then click New Rule.
In the form that opens, define the transform rule.
First, write a name and a description of your choice.
In the condition section, set the parameter, operator, and value. The parameter specifies which property of the request is checked, such as URI Path or HTTP method, and the operator determines the type of comparison.
If several conditions must be met at the same time, use the AND button to add more conditions to the same row. If you want the rule to run when any of several independent cases is met, use the OR button to create a new condition group. You can also enter the condition directly as a text expression in the Expression section at the bottom of the form.
In the Then section, select the header you want from the list.
Then set the value type:
- If the value should be read from an incoming request header, select Incoming Request Header and specify the Origin or Host field.
- If the value is fixed, select Custom Value and enter it directly.
Finally, click Add Rule.
Example: Multiple Subdomains Accessing One API
Suppose you have a central API service, and 10 different subdomains need to send requests (Fetch/XHR) directly to this API. Using the * character in the Access-Control-Allow-Origin header is not recommended at all for security reasons; on the other hand, statically configuring all 10 subdomains on the server requires complex management.
You can create a smart transform rule so the CDN sets the CORS header dynamically based on the client's request.
| Field | Value |
|---|---|
| Parameter | URI Path |
| Operator | Starts with |
| Value | /api/ |
| Header | Access-Control-Allow-Origin |
| Value type | Incoming Request Header |
| Value | Origin |
With this setting, the CDN sets the Access-Control-Allow-Origin header to the Origin of each incoming request. For example, if a request is sent from https://sub1.yourdomain.com, the response returns the same value in the CORS header. This works securely and dynamically for all subdomains, without any change on the origin server.