Filtering URLs | rust-crawler | Spatie       

 SPATIE  

rust-crawler
============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Rust-crawler](https://spatie.be/docs/rust-crawler/v1)  Basic-usage  Filtering URLs

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/rust-crawler/v1) 

  Filtering URLs    
- [ Introduction ](https://spatie.be/docs/rust-crawler/v1/introduction)
- [ Installation &amp; setup ](https://spatie.be/docs/rust-crawler/v1/installation-setup)
- [ Support us ](https://spatie.be/docs/rust-crawler/v1/support-us)
- [ Questions and issues ](https://spatie.be/docs/rust-crawler/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/rust-crawler/v1/changelog)
- [ About us ](https://spatie.be/docs/rust-crawler/v1/about-us)

Basic usage
-----------

- [ Your first crawl ](https://spatie.be/docs/rust-crawler/v1/basic-usage/starting-your-first-crawl)
- [ Crawl responses ](https://spatie.be/docs/rust-crawler/v1/basic-usage/handling-crawl-responses)
- [ Using observers ](https://spatie.be/docs/rust-crawler/v1/basic-usage/using-observers)
- [ Collecting URLs ](https://spatie.be/docs/rust-crawler/v1/basic-usage/collecting-urls)
- [ Filtering URLs ](https://spatie.be/docs/rust-crawler/v1/basic-usage/filtering-urls)
- [ Testing ](https://spatie.be/docs/rust-crawler/v1/basic-usage/testing)
- [ Tracking progress ](https://spatie.be/docs/rust-crawler/v1/basic-usage/tracking-progress)

Configuring the crawler
-----------------------

- [ Concurrency &amp; throttling ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/crawl-behavior)
- [ Limits ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/setting-crawl-limits)
- [ Extracting resources ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/extracting-resources)
- [ Configuring requests ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/configuring-requests)
- [ Response filtering ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/handling-responses)
- [ Respecting robots.txt ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/respecting-robots-txt)

Advanced usage
--------------

- [ JavaScript rendering ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/rendering-javascript)
- [ Custom link extraction ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/extracting-custom-links)
- [ Custom request handlers ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/custom-request-handlers)
- [ Crawling across requests ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/crawling-across-requests)
- [ Custom crawl queue ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/using-a-custom-crawl-queue)
- [ Graceful shutdown ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/graceful-shutdown)

 Filtering URLs
==============

###  On this page 

1. [ Scope helpers ](#content-scope-helpers)
2. [ Inline filtering ](#content-inline-filtering)
3. [ Custom crawl policies ](#content-custom-crawl-policies)
4. [ Always crawl and never crawl ](#content-always-crawl-and-never-crawl)

By default, the crawler only crawls URLs on the same hostname. You can control which URLs are crawled using scope helpers or custom crawl policies.

Scope helpers
-----------------------------------------------------------------------------------------------

Use `include_subdomains(true)` to crawl subdomains too, or `allow_external_links(true)` to permit unrelated hosts:

```
use spatie_crawler::Crawler;

fn with_subdomains() -> spatie_crawler::Result {
    Crawler::builder("https://example.com")?
        .include_subdomains(true)
        .build()
}
```

### Matching www and non-www

By default, `example.com` and `www.example.com` are different hosts. Use `match_www(true)` to treat them as equivalent. This works in both directions.

### Combining match\_www and include\_subdomains

Both settings can be used together. With subdomain matching, `www.` is stripped from both hosts before the comparison, so `blog.example.com` matches a seed of `www.example.com`:

```
use spatie_crawler::Crawler;

fn configured() -> spatie_crawler::Result {
    Crawler::builder("https://www.example.com")?
        .match_www(true)
        .include_subdomains(true)
        .build()
}
```

Host scope does not restrict schemes or ports and does not disable private-network checks. See [network safety](../configuring-the-crawler/configuring-requests#network-safety) before widening access to sites that receive credentials.

Inline filtering
--------------------------------------------------------------------------------------------------------

For custom filtering logic, use `should_crawl` with a closure:

```
use spatie_crawler::Crawler;

fn configured() -> spatie_crawler::Result {
    Crawler::builder("https://example.com")?
        .should_crawl(|url| !url.path().starts_with("/admin"))
        .build()
}
```

The closure runs alongside the built-in scope checks. It must be `Send + Sync + 'static`. Use `move` with owned configuration when it needs captured values.

Custom crawl policies
-----------------------------------------------------------------------------------------------------------------------

For reusable filtering logic, create a type that implements `CrawlPolicy`. Add `url = "2"` to your dependencies for this example:

```
use spatie_crawler::{CrawlPolicy, Crawler};
use url::Url;

struct PublicPages;

impl CrawlPolicy for PublicPages {
    fn should_crawl(&self, url: &Url) -> bool {
        !url.path().starts_with("/private") && url.query().is_none()
    }
}

fn configured() -> spatie_crawler::Result {
    Crawler::builder("https://example.com")?
        .crawl_policy(PublicPages)
        .build()
}
```

Policies are synchronous. Do not block on network or database calls inside them. Scope and custom policy are checked before enqueueing and again for resumed work. The built-in HTTP transport also checks redirect destinations against crawl policy.

Always crawl and never crawl
--------------------------------------------------------------------------------------------------------------------------------------------

Sometimes you need to override your crawl policy for specific URL patterns. Use `exclude_glob` to block matches and `always_crawl_glob` to force them. `include`, `exclude` and `always_crawl` accept regular expressions instead. All patterns match the full URL string; invalid patterns make `build()` fail.

```
use spatie_crawler::Crawler;

fn configured() -> spatie_crawler::Result {
    Crawler::builder("https://example.com/docs")?
        .include(r"/docs(?:/|$)")
        .exclude_glob("*/archive/*")
        .always_crawl_glob("https://cdn.example.com/*")
        .build()
}
```

When includes exist, a URL must match at least one. Exclusions win over includes, and the custom policy must agree too. `always_crawl` matches override all of these, host scope and robots.txt checks. An always-match wins even if an exclude matches the same URL.

Use that override carefully. It does not bypass depth, body-size, timeout or network limits, and is not a switch to ignore document-level robots directives. See [respecting robots.txt](../configuring-the-crawler/respecting-robots-txt).

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
