Set URL/Protocol Rewrite Rules

Last update:2024-07-17 17:19:56

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 facilitates the scenarios described for rewriting request URLs or protocols by responding with a specific status code and a new request URL for the client to initiate a new request.

How to Set Up URL/Protocol Rewriting

  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 온라인 지원센터 변경 안내.
  3. In the configuration modification page, find URL/Protocol Rewriting under Page Rewriting/Redirecting and click on the Add button.
  4. In the rule editing page, you can add the following configuration items as needed.

Effective Range
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, for example: rewrite/*.(jpg|png|gif), which means requests for jpg|png|gif files in the rewrite directory will match this rule.


As shown above, in addition to configuring a URL regular expression as the basic matching condition for your rule’s scope of effect, you can also add some other conditions through Advanced Scope. 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 Exception UA Supports regular expressions, spaces and TABs are translated to \s;
Multiple UAs can be configured simultaneously, each UA on a separate line.
Country or Exception 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 China, Southwest China.
Request Header or Exception 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.
Exception 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 Exception 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 Exception UA 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 Origin Pull Protocol Rewriting for related rules configuration.
In this rule, you can choose HTTP -> HTTPS or HTTPS -> HTTP rewriting as shown below.


Type 2: URL Rewriting
Similar to Protocol Pewrite, 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’s host and port are to be replaced during requesting back to origin, please refer to Origin Pull Host and Port for related rule configuration.
In the type of URL rewriting, you can fill the Original Path with the request URL you want to rewrite, which supporting regular expressions or a complete URL, such as (https://[^/]+)/.* or http://domain/browse/index.html?aa=1; in Redirection Path, fill in the rewritten path, 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. In the case of identical matching conditions, the rule with a higher priority will be applied only.

Besides entering the rule page for detailed parameter configuration by clicking the Add button, you can also click Quick Configuration next to the Add button for simple configuration of protocol or URL rewriting. In the quick configuration, Effect Range 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 redirect 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 Path 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 pull, 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 HTTP—>HTTPS and HTTPS—>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!