Skip to main content
Using regex to block URLs
Updated over 2 weeks ago

This tutorial explains how to make 1Blocker block URLs with the help of regular expressions. The article is worth reading if you want to dive into one of the most powerful features of 1Blocker.

However, if you find this material difficult, you can start right from the beginning: Block Sites - we've prepared several tutorials on creating basic custom rules in the Custom Rules section.

Important: the following rule format is valid only for the 'Block URL' type of rules available in the Advanced section.


While blocking URLs, 1Blocker takes advantage of regular expression patterns or regex. A regular expression represents a sequence of characters written in a special format.

The regex syntax is widely used to extract information from any text by searching for matches of a specific pattern. When we want to block a URL, we create such a pattern and give it to Safari, which processes the block rule and applies it to corresponding web pages.

A quick introduction to regex

In regex, every character is “encoded” and validated symbol by symbol from left to right. We can divide regex characters into two groups:

  • Ordinary characters like letters, digits, and some symbols. When Safari's engine meets an ordinary character, it perceives such a character as it is without changing its function. For example, a regex “blocker” is the most basic pattern, simply matching the literal word “blocker”.

  • Special characters. This is where the power of regex lies. Some regex characters make the engine act in a special way, making the search possibilities more flexible. For example, a dot . matches any character so that three dots may mean dog, cat, run, one, etc.

The following special characters are supported by Safari:

  • . - a dot

  • [] - square brackets

  • () - parentheses

  • ? - a question mark

  • * - an asterisk

  • + - a plus

  • ^ - a caret

  • $ - a dollar sign

Also, there is an important rule you should remember about special characters. If you want such a symbol to be processed as an ordinary character, it must be escaped with a backslash \? or \+, and so on.

For example, let’s try to block the following URL: https://domain.com

There is a dot before “com”, right? So, the engine will think that there is a character before the “com” part, and it might be any symbol, not just a dot. So, we need to put a backslash before the dot to match it literally: https://domain\.com, so that Safari will see that it is literally the dot before the “com” part.

Supported Regex Characters

Now that we've understood the main idea of regular expressions let’s take a closer look at every special character supported by Safari.

Important: these features work only in filters created in the Custom Rules > Advanced section.

  • . (dot) can match any single character (letter, digit, whitespace, anything). If you actually need a dot in a URL, don’t forget to escape it with \.

For instance, https://1bl.cker\.com will block not only https://1blocker.com but also https://1blucker.com, https://1bl1cker.com, etc.


  • [a-c], [abc], or any set of characters inside [square brackets] will only match any combinations of given characters [abc] or a sequential range of characters [a-c] and nothing else. In other words, the rule will be triggered only if any of the characters given in square brackets are found in the corresponding place in the URL.

A couple of examples: the pattern https://1blocker\.[kc]om corresponds either to https://1blocker.сom or https://1blocker.kom.

And speaking of ranges of characters, https://[1-3]blocker\.com will match three websites, https://1blocker.com, https://2blocker.com, and https://3blocker.com.


  • + represents one or more of the characters it follows (it always follows a character or a group of characters).

For instance, we have the following pattern: https://1blo+cker\.сom. It matches https://1blocker.сom, 1blooocker.сom, https://1blooooooocker.com, and so on.


  • * represents either zero or more of the character it follows (it also follows a character or a group of characters). So, as it may represent zero characters, there might be nothing in the corresponding position in the URL.

This example https://1blo*ck*r\.сom matches such URLs as https://1blckr.сom, https://1blocker.сom, https://1blooockeer.сom, and so on.


  • ? allows you to match either zero or one of the preceding characters or group of characters. In other words, it denotes optionality. You can use it in any pattern to match both http and https versions of the same website.

So the pattern https?://domain.сom will block both http://domain.com and https://domain.сom.


  • (abc) can capture any subpattern inside a pair of parentheses as a group, which means we can apply other special symbols, e.g., +, *, or ? to the whole group.

Let’s look at this pattern: https://(.*\.)?domain\.com. It will block not only https://domain.com but its subdomains consisting of any number of characters, for instance, https://ads.domain.com. Let's break down the pattern. Here, we have a group marked by (). The whole group is optional because of ?, which consists of any number (*) of any characters (.) and an escaped dot \.


  • ^ a caret at the beginning of a pattern restricts the URL to start only with the character that follows the caret(^). Also, a caret inside square brackets tells the engine to match all characters except the ones inside the given square brackets.

Here are the examples for both cases:

This pattern ^https://domain\.com will make sure that nothing precedes the letter h, it must be the first character of the string. So, if the target URL is a part of another URL, the rule won’t work: https://anotherdomain.com/https://domain.com.

For example, using caret in square brackets [ ], the pattern https://[^d]omain\.com won’t match domain.com but will work for lomain.com, romain.com, etc.


  • $ a dollar sign marks the end of the string, ensuring that it ends where the dollar sign is located. If the string continues, the check fails, letting Safari load the URL.

For example, this pattern https://1blocker.com/$ blocks the main page of the site 1blocker.com while loading its subpages, such as https://1blocker.com/team, and so on, because the URL continues after the place where the $ sign is located in the initial pattern.

Useful Templates

We would like to give you two basic patterns you can apply to create effective custom blocking rules.

The first one matches any URL, no matter what characters it contains or their total number.

.*

And here's the pattern Apple recommends using to block regular domains, as it blocks subdomains and is balanced enough so you are less likely to see any side effects.

^https?://+([^:/]+\.)?domain\.com[:/]

Let’s break it down into smaller parts:

  • ^https?://+ matches http:// and https://, and it also ensures there is no text before the URL;

  • ([^:/]+\.)? targets all subdomains if there are any;

  • domain\.com matches the domain itself;

  • [:/] blocks the domain even if the URL contains an extra part after .com. For example, domain.com/page or domain.com:8000

Did this answer your question?