Skip to content

Commit

Permalink
Merge pull request #489 from AdguardTeam/feature/AG-4768
Browse files Browse the repository at this point in the history
Add $urltransform modifier description
  • Loading branch information
anyakushin authored May 27, 2024
2 parents 141a16c + caef5e6 commit 01c7465
Showing 1 changed file with 95 additions and 1 deletion.
96 changes: 95 additions & 1 deletion docs/general/ad-filtering/create-own-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ The list of the available modifier options:

:::note

Blocking cookies and removing tracking parameters is achieved by using rules with [`$cookie`](#cookie-modifier) and [`$removeparam`](#removeparam-modifier) modifiers. Exception rules with only `$stealth` modifier will not do those things. If you want to completely disable all Stealth Mode features for a given domain, you need to include all three modifiers: `@@||example.org^$stealth,removeparam,cookie`
Blocking cookies and removing tracking parameters is achieved by using rules with the [`$cookie`](#cookie-modifier), [`$urltransform`](#urltransform-modifier) and [`$removeparam`](#removeparam-modifier) modifiers. Exception rules that contain only the `$stealth` modifier will not do these things. If you want to completely disable all Stealth mode features for a given domain, you must include all three modifiers: `@@||example.org^$stealth,removeparam,cookie`.

:::

Expand Down Expand Up @@ -1117,6 +1117,7 @@ These modifiers are able to completely change the behavior of basic rules.
| [$removeheader](#removeheader-modifier) |||||||
| [$removeparam](#removeparam-modifier) |||||||
| [$replace](#replace-modifier) |||||||
| [$urltransform](#urltransform-modifier) |||||||
| [noop](#noop-modifier) |||||||
| [$empty 👎](#empty-modifier "deprecated") |||||||
| [$mp4 👎](#mp4-modifier "deprecated") |||||||
Expand Down Expand Up @@ -2143,6 +2144,99 @@ Rules with `$replace` modifier are supported by AdGuard for Windows, Mac, and An

:::

#### **`urltransform`** {#urltransform-modifier}

The `$urltransform` rules allow you to modify the request URL by replacing the text matched by the regular expression.

**Features**

- `$urltransform` rules apply to any request URL text.
- `$urltransform` rules can also **modify the query part** of the URL.
- `$urltransform` will not be applied if the original URL is blocked by other rules.
- `$urltransform` will be applied before `$removeparam` rules.

The `$urltransform` value can be empty for exception rules.

**Multiple rules matching a single request**

If multiple `$urltransform` rules match a single request, we will apply each of them. **The order is defined alphabetically.**

**Syntax**

`$urltransform` syntax is similar to replacement with regular expressions [in Perl](http://perldoc.perl.org/perlrequick.html#Search-and-replace).

```text
urltransform = "/" regexp "/" replacement "/" modifiers
```

- **`regexp`** — a regular expression.
- **`replacement`** — a string that will be used to replace the string corresponding to `regexp`.
- **`modifiers`** — a regular expression flags. For example, `i` — insensitive search, or `s` — single-line mode.

In the `$urltransform` value, two characters must be escaped: the comma `,` and the dollar sign `$`. Use the backslash character `\` for this. For example, an escaped comma looks like this: `\,`.

**Examples**

```adblock
||example.org^$urltransform=/(pref\/).*\/(suf)/\$1\$2/i
```

There are three parts in this rule:

- `regexp``(pref\/).*\/(suf)`;
- `replacement``\$1\$2` where `$` is escaped;
- `modifiers``i` for insensitive search.

**Multiple `$urltransform` rules**

1. `||example.org^$urltransform=/X/Y/`
2. `||example.org^$urltransform=/Z/Y/`
3. `@@||example.org/page/*$urltransform=/Z/Y/`

- Both rule 1 and 2 will be applied to all requests sent to `example.org`.
- Rule 2 is disabled for requests matching `||example.org/page/`, **but rule 1 still works!**

**Re-matching rules after transforming the URL**

If the `$urltransform` rule is applied to a request, all the rules will be re-evaluated against the new URL.

E.g., with the following rules:

```adblock
||example.com^$urltransform=/firstpath/secondpath/
||example.com/secondpath^
```

the request to `https://example.com/firstpath` will be blocked before it is sent.

However, `$urltransform` rules will **not be re-applied** in this case to avoid infinite recursion, e.g., with the following rules:

```adblock
||example.com/firstpath^$urltransform=/firstpath/secondpath/
||example.com/secondpath^$urltransform=/secondpath/firstpath/
```

the request to `https://example.com/fisrtpath` will be transformed to `https://example.com/secondpath` and the second rule will not be applied.

**Disabling `$urltransform` rules**

- `@@||example.org^$urltransform` will disable all `$urltransform` rules matching `||example.org^`.
- `@@||example.org^$urltransform=/Z/Y/` will disable the rule with `$urltransform=/Z/Y/` for any request matching `||example.org^`.

`$urltransform` rules can also be disabled by `$document` and `$urlblock` exception rules. But basic exception rules without modifiers do not do that. For example, `@@||example.com^` will not disable `$urltransform=/X/Y/` for requests to **example.com**, but `@@||example.com^$urlblock` will.

:::caution Restrictions

Rules with the `$urltransform` modifier can be used [**only in trusted filters**](#trusted-filters).

:::

:::info Compatibility

Rules with the `$urltransform` modifier are supported by AdGuard for Windows, AdGuard for Mac, and AdGuard for Android **with CoreLibs version 1.15 or higher**.

:::

#### **`noop`** {#noop-modifier}

`noop` modifier does nothing and can be used solely to increase rules' readability. It consists of a sequence of underscore characters (`_`) of any length and can appear in a rule as many times as needed.
Expand Down

0 comments on commit 01c7465

Please sign in to comment.