docs/en/docs/rules/statusCode.md
The statusCode protocol is used to immediately interrupt a request and return a specified HTTP status code, without forwarding the request to the backend server. Through the statusCode protocol, you can:
The statusCode protocol supports multiple configuration methods:
Write the status code to return directly in the rule.
pattern statusCode://code [lineProps...] [filters...]
Example:
www.example.com/api/old-endpoint statusCode://410
Use this method when you need to return different status codes based on conditions or want to reuse configurations.
pattern statusCode://{custom-key} [lineProps...] [filters...]
``` custom-key
404
```
Reference a status code pre-defined in the Values panel (central configuration area).
pattern statusCode://{key-of-values} [lineProps...] [filters...]
Prerequisite: A key named key-of-values with a status code as its value must exist in Values.
Currently NOT supported to dynamically load content from local files or remote URLs.
| Parameter | Required | Description & Examples |
|---|---|---|
| pattern | Yes | Expression used to match the request URL. |
| • Supports domains, paths, wildcards, regular expressions. | ||
| • See Matching Pattern Documentation for details. | ||
| code | Yes | HTTP response status code, such as: |
• 200 OK | ||
• 301 Permanent Redirect | ||
• 302 Temporary Redirect | ||
• 404 Not Found | ||
• 500 Internal Server Error | ||
| • Supports all standard HTTP status codes. | ||
| lineProps | No | Sets additional properties for the rule. |
• Example: lineProps://important can increase this rule's priority. | ||
| • See lineProps Documentation for details. | ||
| filters | No | Optional filter conditions for precise control over when the rule takes effect. |
| • Can match request URL, method, headers, body content. | ||
| • Can match response status code, headers. | ||
| • See Filters Documentation for details. |
# Return 404 status code (Page Not Found)
www.example.com/deleted-page statusCode://404
# Return 500 status code (Internal Server Error)
www.example.com/api/error statusCode://500
# Return 302 Redirect
www.example.com/old-url statusCode://302
# Authentication required when accessing, browser will pop up login dialog.
www.example.com/secure-area statusCode://401
# Disable the login dialog and directly return 401.
www.example.com/secure-area statusCode://401 disable://userLogin
# Or use lineProps to achieve the same effect.
www.example.com/secure-area statusCode://401 lineProps://disableUserLogin
# Return 404 status code with custom response content.
www.example.com/missing-page statusCode://404 resBody://(<h1>Page Not Found</h1>)
# Return 405 (Method Not Allowed) only for specific request methods.
www.example.com/api/resource statusCode://405 includeFilter://m:PUT
# Return different status codes based on request path matching.
/^https?://www\.example\.com/user/\d+/profile/ statusCode://403
# Return status code based on request header conditions.
www.example.com/api statusCode://429 includeFilter://reqH:user-agent=/bot/i
``` maintenance-status
503
```
www.example.com statusCode://{maintenance-status}
``` testing-403
403
```
# Return 403 only in testing environment.
test.example.com/api/admin statusCode://{testing-403}
| Status Code | Scenario Description | Typical Use |
|---|---|---|
| 200 | Successful Response | Test normal flow. |
| 301/302 | Redirect | Test URL redirection logic. |
| 400 | Bad Request | Test client error handling. |
| 401 | Unauthorized | Test authentication flow. |
| 403 | Forbidden | Test access control. |
| 404 | Not Found | Test resource not found handling. |
| 429 | Too Many Requests | Test rate limiting logic. |
| 500 | Internal Server Error | Test server-side exception handling. |
| 503 | Service Unavailable | Test maintenance page. |
statusCode is empty.resBody protocol.401 status code, the browser authentication popup is triggered by default.disable://userLogin.lineProps://disableUserLogin.enable protocol.301, 302, you need to specify the redirect address with the location response header:
www.example.com/old statusCode://302 resHeaders://location="https://www.example.com/new"
# Return different status codes based on time (maintenance window).
www.example.com/api statusCode://503 includeFilter://t:>=00:00&t:<=06:00
# Return 403 with a custom error page.
www.example.com/blocked statusCode://403 resBody://<h1>Access Denied</h1> resHeaders://content-type="text/html; charset=utf-8"
# Return 503 and set retry time.
www.example.com/down statusCode://503 resHeaders://retry-after="3600"
# Randomly return different error status codes to test client fault tolerance.
www.example.com/api/unstable statusCode://500 includeFilter://chance:0.5
www.example.com/api/unstable statusCode://429 includeFilter://chance:0.3
www.example.com/api/unstable statusCode://200 includeFilter://chance:0.2
statusCode: Takes effect during the request stage, does not forward to the backend server.replaceStatus: Takes effect during the response stage, first requests the backend server, then replaces the returned status code.A: Check:
A: For 401 status code:
disable://userLogin or lineProps://disableUserLogin is added.A: Check:
Content-Type response header is correct.A: Check:
Location response header is set.