Back to articles

How do I use regular expressions (Regex) in Fasterize?

Regular expressions (or Regex) are a powerful tool for precisely targeting pages in Fasterize. They allow you to create conditional configuration rules, for example to apply certain optimizations to a specific set of URLs, or conversely to exclude certain ones.Using the “Matches a regex” option in the rule creation interface, you can define very precise dynamic URL patterns. Let’s take a look at how this works, using concrete examples.

Introduction

Regular expressions (or Regex) are a powerful tool for precisely targeting pages in Fasterize.

They allow you to create conditional configuration rules, for example, to apply certain optimizations to a specific set of URLs, or to exclude certain ones.

Using the “Matches a regex” option in the rule creation interface, you can define very specific dynamic URL patterns. Let’s take a look at how this works, using some concrete examples.

Understanding the Basics of Regular Expressions

A regular expression is a sequence of characters that describes a text pattern.

This allows you to say, for example: “I want to target all URLs that start with /products/ “and end with a word.”

💡 In short: A regex is a smart filter based on the structure of a text (in this case, your website’s URLs).

Let's take a simple example:

The Regex ^/products/.* means:

This allows you to target all the pages in the "Products" section of a website.

Key Elements of Regular Expression Syntax

Here are some useful basics for writing your own regular expressions:

💡 Basic Glossary:

Apply a regular expression in Fasterize

In Fasterize, you can use a regular expression:

In the interface, this is done through the rule configuration menu by selecting the “Matches a regex” option from the list of targeting types.

Example of use: You want to exclude pages /blog/ For HTML minification, create a rule using:

Practical examples of regular expressions for your pages

Here are some practical examples of how to use regular expressions for an e-commerce site:

Target all product pages

URL format: /products/product-name

Regex: ^/products/.*

Matches all pages whose URL begins with /products/.

Target a single product page

URL format: /single-product

Regex: ^/product-only$

Matches exactly this URL, and only this one.

Target specific subcategories

URL format: /category1/subcategory2/myproduct

Regex: ^/category1/subcategory2/[^/]+$

Target all pages at this level of depth, without any additional subdirectories.

Target general category pages

URL format: /category1/

Regex: ^/category1/[^/]*$

Ideal for targeting category or product listing pages.

💡 Regex Reference Table:

Use CaseExample URLRegexProduct Section/products/myproduct^/products/.*Specific product page/single-product^/single-product$Category with product subcategory/cat1/subcat2/product^/cat1/subcat2/[^/]+$General category page/category1/^/category1/[^/]*$

Pay attention to the order of the rules in EdgeSpeed

In Fasterize (via the EdgeSpeed engine), the order of the rules is crucial:

The system applies the first rule that matches the request URL.

Let’s take the following example:

A URL such as /staff also matches ^/personal, so Rule 1 will be applied, even though Rule 2 is more specific.

⚠️ To prevent this behavior, place the most specific rules at the top of the list.

In the correct order:

^/staff
^/personal

Incorrect order:

^/personal
^/staff

Conclusion

Regular expressions are a simple yet powerful tool for fine-tuning your rules in Fasterize.

They allow you to precisely target the pages where you want to apply or disable certain optimizations.

💡 Key takeaway:

For further information, feel free to check out an online testing tool (e.g., regex101.com)