Filtering URLs | crawler-go | Spatie       

 SPATIE  

crawler-go
==========

spatie.be/open-source

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

 Version   v1      

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

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

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

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

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

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

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

- [ JavaScript rendering ](https://spatie.be/docs/crawler-go/v1/advanced-usage/rendering-javascript)
- [ Custom link extraction ](https://spatie.be/docs/crawler-go/v1/advanced-usage/extracting-custom-links)
- [ Custom request handlers ](https://spatie.be/docs/crawler-go/v1/advanced-usage/custom-request-handlers)
- [ Crawling across requests ](https://spatie.be/docs/crawler-go/v1/advanced-usage/crawling-across-requests)
- [ Custom crawl queue ](https://spatie.be/docs/crawler-go/v1/advanced-usage/using-a-custom-crawl-queue)
- [ Graceful shutdown ](https://spatie.be/docs/crawler-go/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 profiles ](#content-custom-crawl-profiles)
4. [ Always crawl and never crawl ](#content-always-crawl-and-never-crawl)

By default, the crawler will crawl every URL it finds, including links to external sites. You can control which URLs are crawled using a profile.

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

```
c.Profile = crawler.InternalURLs{
    BaseURL: "https://example.com",
    MatchWWW: true,
    IncludeSubdomains: true,
}
```

Without the two booleans, only the same hostname matches. Schemes and ports do not affect hostname matching. `MatchWWW` treats `example.com` and `www.example.com` as equivalent in both directions. `IncludeSubdomains` strips `www.` before checking subdomains, so `blog.example.com` matches a base of `www.example.com`. Hostname comparisons are case-insensitive.

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

```
c.Profile = crawler.ProfileFunc(func(u string) bool {
    return !strings.Contains(u, "/admin")
})
```

Custom crawl profiles
-----------------------------------------------------------------------------------------------------------------------

For reusable filtering logic, implement `Profile`:

```
type MyProfile struct{}

func (MyProfile) ShouldCrawl(raw string) bool {
    u, err := url.Parse(raw)
    return err == nil && u.Hostname() == "example.com" &&
        !strings.Contains(u.Path, "/private")
}
```

Set `c.Profile = MyProfile{}`. The default is `AllURLs{}`. Profiles run before adding discoveries and again before scheduling resumed URLs. They run on the scheduler and should not block.

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

```
c.AlwaysCrawl = []string{"https://cdn.example.com/*"}
c.NeverCrawl = []string{"*/admin/*", "*/tmp/*"}
```

Patterns match the full URL. `*` matches any characters including slashes and `?` matches one character. Unlike PHP's `fnmatch`, bracket character classes and backslash escapes are not supported.

Always-crawl wins over never-crawl, the profile and robots.txt. It does not bypass depth limits, document noindex/nofollow, HTTP(S) validation, timeouts or response limits.

Only crawl sites you trust or are authorized to crawl. Profiles do not check redirect hops, DNS answers or browser subresources. They are not an SSRF boundary. Use network isolation for hostile input. Headers and query defaults apply to every initial request, including external URLs, so restrict scope before adding credentials.

 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)
