Set URL/Protocol Rewrite Rules

Last update:2026-05-08 15:11:57

When users access your website content through the CDNetworks CDN platform, you can configure simple rules in the console, which allow you to deliver specific content to users based on various criteria such as the protocol of the request URL, different request contents, request headers, or even the geographical location of the users. Currently, CDNetworks supports URL or protocol rewrite scenarios by returning a specific status code and a new URL, prompting the client to initiate a new request.

How to Set Up URL/Protocol Rewrite Rules

  1. Log in to the CDNetworks Console and select the appropriate product.
  2. Navigate to the Configuration page, locate the domain you wish to configure, and click on Modify Configuration above it or the edit button to the right of the domain name Cloud Security Product Maintenance Announcement.
  3. In the configuration modification page, find Redirects under Page Rules and click on the Add button.
  4. In the rule editing page, you can add the following configuration items as needed.

Apply to
CDNetworks allows you to enter a URL regular expression in the console to determine if a user’s request fits your rewriting rule through URL Pattern (Regex), for example: rewrite/*.(jpg|png|gif), which means requests for jpg|png|gif files in the rewrite directory will match this rule.

Advanced Scope Conditions
In addition to configuring a URL Pattern as the basic matching condition for your rule’s scope of effect, you can also add some other conditions through Advanced Scope by clicking Advanced Scope Conditions. You can select one or more of the following parameters as composite configuration items, in conjunction with the basic URL regex, forming an AND relationship to determine the final scope of your rule.

Parameter Description
UA or Exclude User-Agent Supports regular expressions, spaces and TABs are translated to \s;
Multiple UAs can be configured simultaneously, each UA on a separate line.
Country or Exclude Country Supports direct search selection from the list of countries/regions provided by CDNetworks;
For Mainland China, it is possible to select specific provinces or larger geographical areas, such as East Region, Southwest Region.
Request Header or Exclude Request Header Enter the header and its value, and the value supports regular expressions, for example: Range bytes=[0-9]{9,};
Currently, a single rule only supports configuring one request header.
Exclude URL (Regex) Supports regular expressions;
If URL Pattern is filled with rewrite/*.(jpg|png|gif), and this item is filled with rewrite/*exception*.jpg, it means requests for jpg|png|gif files in the rewrite directory will match this rule, but requests containing *exception*.jpg will not apply to this rule.

The Advanced Scope items marked with Exclude imply NOT, excluding specific cases within the basic matching rule scope; each configuration item listed above can appear only once in one single rule, and UA and Exclude User-Agent are mutually exclusive in a single rule, as are Country and Request Header, thus a maximum of four advanced configuration items can exist simultaneously.

Rewrite Type
Currently, CDNetworks allows you to choose between Protocol Rewrite or URL Rewrite. Regardless of the type you select, you need to choose the response status code to return to the client, with CDNetworks supporting a range of response status codes: 301;302;303;307, the default value being 302.

Type 1: Protocol Rewrite
Protocol rewriting refers to the CDN edge node rewriting the protocol upon receiving a client request. The node uses the rewritten protocol for caching and origin pull. If only the protocol is rewritten during requesting back to origin, please refer to HTTP/HTTPS Back to Origin for related rules configuration.
In this rule, you can choose Force HTTPS or Force HTTP.

Type 2: URL Rewrite
Similar to Protocol Rewrite, URL rewriting is performed when the CDN node receives a request, using the rewritten URL for caching and origin pull. If only the request URL host and port are to be replaced during requesting back to the origin, please refer to Host Header and Port for related rule configuration.
For URL Rewrite, enter the request URL to rewrite in Original URL. It can be a regular expression or a complete URL, such as (https://[^/]+)/.* or http://domain/browse/index.html?aa=1. In Target URL, enter the rewritten URL, which must start with http:// or https://.

Priority
You can set the priority of this rule from 1-10, with higher numbers indicating higher match and apply priority. If multiple rules have identical matching conditions, only the rule with the higher priority will be applied.

Besides entering the rule page for detailed parameter configuration by clicking the Add button, you can also click Quick Settings next to the Add button for simple configuration of protocol or URL rewriting. In the quick configuration, Apply to will default to all requests, the response status code to 302, and the rest of the configurations, such as the protocol to rewrite, original URL and target URL, priority, can all be customized.

After completing the above configuration, please click Confirm, then select Next to submit your configuration. To avoid affecting your production environment, we recommend Pre-deploying first to verify your configuration in the test environment. Once the configuration is confirmed to be correct, click Deploy Directly to officially activate the configuration in the live environment, which typically takes about 3-5 minutes to take effect. For more details on pre-deployment testing, please refer to the tutorial Verifying Configuration Effectiveness Through Pre-deployment.

Configuration Examples

Example 1: Protocol rewriting for requests matching the URL regular expression and UA, Country fields
For the domain associated with this rule, when a request matches the URL regular expression shown in the figure, and the UA header is Mozilla/4.0 with the user’s country as Japan; South-Korea, the CDN servers will cache and return the URL under the rewritten protocol to the client. For example, a request for http://domain/rewrite/example.jpg will return https://domain/rewrite/example.jpg, with a response status code of 303, prompting the client to initiate a new request to the new protocol’s URL.


Example 2: URL rewriting for requests matching the URL regular expression and UA, Country fields, specific URL
For the domain associated with this rule, when a request matches the URL regular expression shown in the figure, and the UA header is Mozilla/4.0 with the user’s country as Japan; South-Korea, and matching the Original URL specified, the CDN servers will cache and return the rewritten URL to the client. For example, a request for https://domain/rewrite/urltype1.jpg will return https://domain/target/urltype1.jpg, with a response status code of 302, prompting the client to initiate a new request to the returned URL.


Notes

Since the node uses the rewritten URL for caching and origin request, the rewritten domain must be a domain accelerated on the CDNetworks CDN platform.
For Protocol Rewrite types, only one rule is allowed per domain. This is mainly to prevent a potential loop issue that could occur if a request simultaneously hits both Force HTTPS and Force HTTP. To configure multiple Protocol Rewrite rules, please contact technical support.

Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!