Usage | robots-txt-rust | Spatie      

 SPATIE  

robots-txt-rust
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Robots-txt-rust](https://spatie.be/docs/robots-txt-rust/v1)  Usage

 Version   v1      

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

  Usage    
- [ Introduction ](https://spatie.be/docs/robots-txt-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/robots-txt-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/robots-txt-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/robots-txt-rust/v1/usage)
- [ Testing ](https://spatie.be/docs/robots-txt-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/robots-txt-rust/v1/changelog)
- [ PHP compatibility ](https://spatie.be/docs/robots-txt-rust/v1/compatibility)

 Usage
=====

###  On this page 

1. [ Checking a page ](#content-checking-a-page)
2. [ Parsing robots.txt ](#content-parsing-robotstxt)
3. [ Robots meta tags ](#content-robots-meta-tags)
4. [ X-Robots-Tag headers ](#content-x-robots-tag-headers)

Checking a page
-----------------------------------------------------------------------------------------------------

```
let robots = Robots::create();

robots.may_index("https://www.spatie.be/nl/admin")?;
robots.may_follow_on("https://www.spatie.be/nl/admin")?;
```

`may_index` first checks robots.txt. If crawling is allowed, it reads the page and checks its robots meta tag and global X-Robots-Tag headers. `may_follow_on` checks only the page's meta tag and global headers, not robots.txt.

You can also specify a user agent:

```
let robots = Robots::create().user_agent("UserAgent007");
```

`may_index_for(url, "google")` overrides robots.txt rule selection for one call. Like PHP, this does not change the HTTP User-Agent or select agent-specific response headers. Inspect `RobotsHeaders` directly when you need those.

By default, `Robots` will look for a `robots.txt` file on `https://host.com/robots.txt`. The page's scheme, host and port are preserved. Another location can be specified like so:

```
let robots = Robots::create()
    .with_txt_from("https://www.spatie.be/robots-custom.txt");

let robots = Robots::create()
    .with_txt_from("public/robots.txt");
```

Use `with_txt` for already parsed content:

```
let robots_txt = RobotsTxt::new("User-agent: *\nDisallow: /admin");
let robots = Robots::create().with_txt(robots_txt);

robots.may_index("https://www.spatie.be/admin")?; // false; no page request
```

HTTP methods are blocking, use rustls for HTTPS, follow up to ten redirects and time out after 60 seconds. In async applications, use a blocking worker or supply already fetched strings to the parsers below. Fetch only trusted URLs; the convenience methods do not filter private network destinations.

Parsing robots.txt
------------------------------------------------------------------------------------------------------------

```
let robots_txt = RobotsTxt::new("
  User-agent: *
  Disallow: /admin
  Crawl-delay: 1.5
");

robots_txt.allows_for("/admin", "google"); // false
robots_txt.why_disallows("/admin", "google"); // Some(reasons)
robots_txt.crawl_delay(); // Some("1.5")
```

`allows(url)` uses the wildcard agent. `allows_for(url, user_agent)` accepts paths or full URLs, includes query strings (even an empty `?`), and excludes fragments. Paths are case-sensitive. `*` matches any number of characters, and a final `$` anchors a pattern to the end. The longest matching rule wins; an allow wins a tie. Pattern weights count bytes, including `*` and `$`, as in PHP.

By default, agent names are matched exactly after trimming and lowercasing. When no group matches, the wildcard group is used. Allow and disallow group selection are independent; an exact group without any allow entries can still use the wildcard group's allows. Repeated groups replace previous rules rather than merging them.

For partial user-agent matching:

```
let robots_txt = RobotsTxt::new(content).with_partial_matches();

robots_txt.allows_for("/no-agents", "Mozilla/5.0 (compatible; UserAgent007/1.1)"); // false
```

Partial matching combines all contained agent names and the wildcard group. Chain `ignore_global_group()` to exclude wildcard rules and delays; `include_global_group()` restores them. `exact_matches_only()` restores exact rule matching. These consuming setters return the configured value.

`crawl_delay_for("bing")` returns an exact agent's delay with wildcard fallback, regardless of partial rule matching. Delays remain strings so fractional values such as `"1.5"` are preserved.

`why_disallows(path, user_agent)` returns `None` when allowed, or a list of `Disallow { user_agent, base_path }` reasons. Like PHP, reasons use literal prefix checks, list the exact agent before the global group, and do not normalize the diagnostic agent argument. Wildcard-only denials can return an empty list; use `allows_for` for the decision.

`new(content)` and `create(content)` parse literal strings. `read_from(source)` loads a file or HTTP(S) URL. Like PHP, an unreadable robots.txt becomes empty and allows crawling. To handle loading errors explicitly:

```
let robots_txt = RobotsTxt::try_read_from(source)?;
```

Robots meta tags
--------------------------------------------------------------------------------------------------------

```
let robots = RobotsMeta::create("<meta name=\"robots\" content=\"noindex, nofollow\">");

robots.may_index(); // false
robots.may_follow(); // false
robots.noindex(); // true
robots.nofollow(); // true
```

`read_from(source)?` reads HTML from a file or URL. Like PHP, only the first tag starting with `<meta name="robots"` is considered. Single quotes, mixed case and multiline attributes work. Attributes in a different order do not match. Only `noindex` and `nofollow` substrings are checked; `none` is not expanded in meta tags.

X-Robots-Tag headers
--------------------------------------------------------------------------------------------------------------------

```
let robots = RobotsHeaders::create([
    "X-custom: test",
    "X-Robots-Tag: nofollow",
    "X-Robots-Tag: google: noindex, nofollow",
]);

robots.may_index(); // true
robots.may_follow(); // false
robots.may_index_for("google"); // false
robots.may_follow_for("google"); // false
```

`none` disables both indexing and following in headers. The `noindex`, `nofollow` and `none` predicates each have a `_for(agent)` variant. Header names and directives are case-insensitive; agent keys are case-sensitive. The last header for a given agent replaces earlier ones. Specific-agent directives replace global directives, including explicit false values. These are the PHP package's rules, not a promise of browser or search-engine equivalence.

Use `from_pairs` for `(name, value)` pairs, including repeated header names. Use `read_from(url)?` to fetch a response's headers. Meta/header reads and page checks return `Result` on loading errors, unlike the fail-open robots.txt reader.

See [PHP compatibility](compatibility) for the reference revision, verification and language differences.

 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)
