pathReplace
Provides functionality similar to JavaScript's String.replace() method, dynamically modifying the path portion of a URL using regular expressions or string matching.
URL structure:
txthttps://www.example.com:8080/path/to/resource?query=string \___/ \_____________/\____/\____________________________/ | | | | Protocol (scheme) Host (host) Port (path) Path (path)
The
path
section refers topath/to/resource?query=string
, which excludes the leading/
Rule Syntax
pattern pathReplace://value [filters...]
Parameters | Description | Detailed Documentation |
---|---|---|
pattern | Expression to match request URLs | Match Pattern Documentation |
value | Operation data object, supported from the following channels: • Directory/file path • Remote URL • Inline/embedded/Values content | Operation Instruction Documentation |
filters | Optional filters, supporting matching: • Request URL/Method/Header/Content • Response Status Code/Header | Filter Documentation |
Configuration Example
www.example.com/path pathReplace://123=abc
Visit https://www.example.com/path/123?test=123&value=123
. The server receives the following URL: https://www.example.com/path/abc?test=abc&value=abc
Replace Multiple Strings
``` test.json
test: name
123: abc
```
www.example.com/path2 pathReplace://{test.json}
Visit https://www.example.com/path2/123?test=123&value=123
. The server receives the following URL: URL: https://www.example.com/path2/abc?name=abc&value=abc
Local/Remote Resources
www.example.com/path1 pathReplace:///User/xxx/test.json
www.example.com/path2 pathReplace://https://www.xxx.com/xxx/params.json
# Editing a Temporary File
www.example.com/path3 pathReplace://temp/blank.json
Notes
The following configuration is intended to remove a specific path segment:
www.example.com/api/ pathReplace://(/api/=/)
Expected outcome:
Replace /api/
in https://www.example.com/api/xxx
with /
, resulting in https://www.example.com/xxx
.
Actual issue:
Whistle interprets /api/
as a regular expression rather than a plain string, causing extra slashes to appear after replacement:https://www.example.com///xxx
.
Even if
/api/
is treated as a string, it cannot matchapi/xxx/...
. The path matched by pathReplace does not contain the leading/
Solution:
www.example.com pathReplace://(/^api//=)
Regular expressions in Whistle rules do not require escaping
/
.New versions of Whistle can also use
delete://pathname.0
to delete theapi/
path segment in the above URL. For details, see delete://pathname.xxx
Related Protocols
- Modify request parameters: urlParams
- Delete the path: delete://pathname.xxx
- Delete request parameters: delete://urlParams.xxx