What is the Path Matcher?
The Path matcher allows you to create rules that match requests based on the URL path component. This is one of the most commonly used matchers as it lets you control how different sections of your website or application are handled.
How Path Matcher Works
The Path matcher evaluates the path portion of the URL (everything after the domain name and before any query parameters). For example, in the URL https://example.com/blog/articles?id=123
, the path is /blog/articles
.
Available Operators
The Path matcher supports these comparison operators:
Operator | Description | Example |
| Exact path match |
|
| Path does not match exactly |
|
| Path begins with the value |
|
| Path ends with the value |
|
| Path contains the specified value |
|
| Path does not contain the value |
|
| Path exists (any non-empty value) | Matches any request with a path |
| Path does not exist | Matches requests to the root path |
API Reference
When working with Rules through the API, Path matchers are represented in JSON format. This is the same structure used in the API endpoints for creating and updating rules.
Configuration Examples
Example 1: Basic Blog Redirect
This matcher will match any path that starts with /blog/
:
βIn the UI:
In the API:
{
"type": "path",
"value": "/blog/",
"operator": "starts_with"
}
Example 2: Excluding Admin Section
This matcher will match paths that do not contain "admin":
In the UI:
In the API:
{
"type": "path",
"value": "admin",
"operator": "does_not_contain"
}
Example 3: PDF File Handling
This matcher will match any path that ends with ".pdf":
In the UI:
In the API:
{
"type": "path",
"value": ".pdf",
"operator": "ends_with"
}
Example 4: Specific Page Match
This matcher will match exactly the "/contact" page:
In the UI:
In the API:
{
"type": "path",
"value": "/contact",
"operator": "is"
}
Example 5: Homepage Only
This matcher will match only the root path:
In the UI:
In the API:
{
"type": "path",
"value": "/",
"operator": "is"
}
Common Use Cases
Here are some popular ways to use the Path matcher:
Redirecting Legacy URLs
If you've restructured your website, you can redirect old URLs to new ones:
Create a rule with a Path matcher using
is
operator for the old pathAdd a Set Response Header handler and set the Location header to the new URL
Add a Static Response handler with a 301 status code
Section-Specific Headers
Add special headers to specific sections of your site:
Create a rule with a Path matcher using
starts_with
for your section (e.g.,/docs/
)Add a Set Response Header handler to add custom caching or security headers
Serving Static Content
Create a rule to serve static content for specific paths:
Create a rule with a Path matcher using
starts_with
for static content paths (e.g.,/static/
)Add a Static Response handler to serve the content directly
Protecting Admin Areas
Add authentication requirements to admin sections:
Create a rule with a Path matcher using
starts_with
for admin paths (e.g.,/admin/
)Add handlers to verify authentication headers or redirect to a login page
Combining with Other Matchers
The Path matcher becomes even more powerful when combined with other matchers. For example:
Path + Method: Match only GET requests to specific paths
Path + Header: Match requests to specific paths that include certain headers
Path + Query: Match paths that also include specific query parameters
Remember that when multiple matchers are used in a rule, ALL must match to apply the rule (AND logic).
Best Practices
Be precise with your path patterns to avoid unintended matches
For root domain matching, use
is
with value/
Use
starts_with
for section-based rules (like/blog/
,/api/
, etc.)Consider case sensitivity - paths are matched exactly as specified
Test your path matchers thoroughly with different URL variations
Order your rules carefully since multiple matching rules will execute in order
Wildcards and Path Matching
While not using regex directly, the Path matcher provides wildcard-like functionality through its operators:
starts_with
acts like a trailing wildcard (/blog/*
)ends_with
acts like a leading wildcard (*.pdf
)contains
acts like wildcards on both sides (*/blog/*
)
Troubleshooting
If your Path matcher isn't working as expected:
Check for exact spelling and case matching
Verify that you've chosen the correct operator
Ensure your path starts with a forward slash
/
when appropriateTest with and without trailing slashes as they are considered different paths
With the Path matcher, you can create sophisticated routing rules based on URL paths. As you become more familiar with it, you'll discover many ways to enhance your domain's behavior without modifying your application code.
Until next time, keep building!
Need more help? Reach out via the Intercom chat widget and we'll be right with you!