PHP Class for parsing robots.txt files according to Google, Yandex specifications.

Installs: 32 738

Dependents: 2

Suggesters: 0

Security: 0

Stars: 35

Watchers: 5

Forks: 13

Open Issues: 4

Language:DIGITAL Command Language

v2.4.1 2021-05-11 17:44 UTC

This package is auto-updated.

Last update: 2021-10-11 19:01:21 UTC


Build Status

RobotsTxtParser — PHP class for parsing all the directives of the robots.txt files

RobotsTxtValidator — PHP class for check is url allow or disallow according to robots.txt rules.

Try demo of RobotsTxtParser on-line on live domains.

Parsing is carried out according to the rules in accordance with Google & Yandex specifications:

Last improvements:

  1. Pars the Clean-param directive according to the clean-param syntax.
  2. Deleting comments (everything following the '#' character, up to the first line break, is disregarded)
  3. The improvement of the Parse of Host — the intersection directive, should refer to the user-agent '*'; If there are multiple hosts, the search engines take the value of the first.
  4. From the class, unused methods are removed, refactoring done, the scope of properties of the class is corrected.
  5. Added more test cases, as well as test cases added to the whole new functionality.
  6. RobotsTxtValidator class added to check if url is allowed to parsing.
  7. With version 2.0, the speed of RobotsTxtParser was significantly improved.

Supported Directives:

  • DIRECTIVE_ALLOW = 'allow';
  • DIRECTIVE_DISALLOW = 'disallow';
  • DIRECTIVE_HOST = 'host';
  • DIRECTIVE_SITEMAP = 'sitemap';
  • DIRECTIVE_USERAGENT = 'user-agent';
  • DIRECTIVE_CRAWL_DELAY = 'crawl-delay';
  • DIRECTIVE_CLEAN_PARAM = 'clean-param';
  • DIRECTIVE_NOINDEX = 'noindex';


Install the latest version with

$ composer require bopoda/robots-txt-parser

Run tests

Run phpunit tests using command

$ php vendor/bin/phpunit

Usage example

You can start the parser by getting the content of a robots.txt file from a website:

$parser = new RobotsTxtParser(file_get_contents(''));

Or simply using the contents of the file as input (ie: when the content is already cached):

$parser = new RobotsTxtParser("
	User-Agent: *
	Disallow: /ajax
	Disallow: /search
	Clean-param: param1 /path/file.php

	User-agent: Yahoo
	Disallow: /


This will output:

array(2) {
  array(3) {
    array(2) {
      string(5) "/ajax"
      string(7) "/search"
    array(1) {
      string(21) "param1 /path/file.php"
    string(11) ""
  array(1) {
    array(1) {
      string(1) "/"

In order to validate URL, use the RobotsTxtValidator class:

$parser = new RobotsTxtParser(file_get_contents(''));
$validator = new RobotsTxtValidator($parser->getRules());

$url = '/';
$userAgent = 'MyAwesomeBot';

if ($validator->isUrlAllow($url, $userAgent)) {
    // Crawl the site URL and do nice stuff


Feel free to create PR in this repository. Please, follow PSR style.

See the list of contributors which participated in this project.

Final Notes:

Please use v2.0+ version which works by same rules but is more highly performance.