Adblock Plus and (a little) more

Rewriting URL instead of blocking · 2018-05-18 18:17 by Hubert Figuière

Starting with Adblock Plus 3.2 for Chrome, Firefox and Opera (and development builds as of 3.1.0.2053), there is a new $rewrite filter option to rewrite the URL of a resource instead of blocking it.

When Adblock Plus matches a request URL with a filter that has the $rewrite option, it will transform the URL following the provided rule, and tell the browser to load the resource using this new URL instead.

The syntax of the rewrite rule is as follow: you specify a string that serves as a template for the new URL. $n gets substituted with the regular expression n-th submatch of the filter. This is the exact same syntax as JavaScript’s String.prototype.replace() function. If the resulting URL is relative (i.e. it doesn’t have a host), the origin of the original query will be used as the base. In any case, if the new URL doesn’t share the origin, the rewrite will be considered as being failed and initial query will be let through.

Furthermore, $rewrite filters are ignored for requests of the type SCRIPT, SUBDOCUMENT, OBJECT and OBJECT_SUBREQUEST, for security reasons.

This option is convenient to modify or strip query parameters.

Example:

||example.com/ad.gif$rewrite=/puppies.gif
This will simply rewrite the requests for “example.com/ad.gif” to be a request for “example.com/puppies.gif”

/(^https?:\/\/example\.com\/page-123\.php)\?.*/$rewrite=$1
This will rewrite the request to example.com/page-123.php stripping the query string: $1 matches “example.com/page-123.php”.

/(\/article\.asp\??)(.*&)?tracker=[^&]*&?(.*)?/$rewrite=$1$2$3
The will remove the “tracker” query parameter from the request URL.

Update (2019-04-19):
Starting with Adblock Plus 3.5, the $rewrite filter option can also redirect the request to a set of internal resources. Starting with Adblock Plus 3.5.2, the $rewrite filter option can only be used with internal resources. For more information, read the updated documentation.

To the install page

Tags:

Comment [7]

  1. Arnoh · 2018-05-25 11:37 · #

    How to rewrite “http://ads.com/banner.gif” to “http://example.com/pixel.jpg” ?

    Reply from Hubert Figuière:

    You can’t do that. The $rewrite option ensure the origin is kept and will simply ignore the rewrite if the new URL isn’t of the same origin.

  2. cjxlist · 2018-05-27 04:02 · #

    $rewrite is very useful to skip video ads or to avoid warning messages on some websites in China,for now.See EasyList China ​https://easylist-downloads.adblockplus.org/easylistchina.txt Already have $rewrite filters to skip video ads on ​http://www.iqiyi.com/ to avoid warning messages on ​https://www.mgtv.com/ ​https://v.qq.com/ ​http://www.dnvod.tv/
    These websites are very popular in china,could you releasing an ABP officials version ASAP?
    As an maintainer of EasyList China,I had received tons of issue reports about these websites.
    Thanks.

  3. Arnoh · 2018-05-29 01:31 · #

    @Hubert Figuière It will be useful if improved this filter to rewrite the URL to a null file (image, script, iframe, xmlhttprequest, stylesheet ….).

    ||example.com/banner.gif$rewerite=null-image

    null-image = R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==

    See https://adblockplus.org/forum/viewtopic.php?p=82629

    Reply from Hubert Figuière:

    What you want is the redirect option that uBlock has. https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#redirect

    The rewrite option isn’t it.

  4. Arnoh · 2018-05-29 23:19 · #

    or for all types of requests ||example.com/banner.gif$rewerite=abp-null

  5. James Edward Lewis II · 2018-06-10 02:42 · #

    The first example of the rewrite syntax would be ignored because the request would be of type SCRIPT.

    Reply from Hubert Figuière:

    Good call. We added the restrictions on content, but forgot to update the examples in the article. They new are updated.

    Thank you so much for pointing it out.

  6. Fanboy · 2018-06-15 10:54 · #

    Should this work? (changing the token from true to false on twitch)

    ||twitch.tv/api/channels/opl/access_token?adblock=true$rewrite=/access_token?adblock=false

  7. med · 2018-06-15 13:52 · #

    @Fanboy
    Your filter is correct but unfortunately request types SCRIPT, SUBDOCUMENT^, OBJECT
    and OBJECT_SUBREQUEST are not rewrited and will be ignored :-/

    see https://issues.adblockplus.org/ticket/6704

    Reply from Sebastian Noack:

    This is true, but is that actually the case here? When I go to Twitch, the requests Fanboy appears trying to target seem to be of the type XMLHTTPREQUEST which should be possible to rewrite.

Commenting is closed for this article.