How robots.txt works
The file always sits in the same place, at the root of the domain, for example yoursite.com/robots.txt. You can open it in a browser for any website. It contains a few simple lines: who the rule applies to (for example all robots or just Googlebot) and which folders they should not crawl. It often also includes a link to the sitemap.
A search engine robot reads the file before it starts crawling the site. Well-behaved robots such as Google, Bing or Seznam follow it. Malicious bots ignore it, so it offers no protection against attackers.
What robots.txt is good for
- blocking crawling of the admin area, basket, checkout and user account,
- stopping robots from crawling endless combinations of filters and sorting in an online shop,
- steering the robot to spend its time on important pages.
What robots.txt does not do
Blocking a page in robots.txt does not mean it disappears from Google. Google will not crawl it, but if links point to it, it may still show it in results without a description. If you really want to exclude a page from results, use a "noindex" tag on the page itself. Google will only see that tag if you do not block the page in robots.txt. You can read more about how pages get into a search engine under indexing.
Robots.txt is not a place to hide sensitive data either. The file is public, so anyone can read a list of "secret" folders in it.
Example
A clothing shop has filters for colour, size, brand and price. Each combination creates a new address, resulting in hundreds of thousands of variants of the same page. Google spent its time on them and discovered new products late. After the filter URLs were blocked in robots.txt, the robot focused on categories and products.
The most common and worst mistake
The line "Disallow: /" blocks robots from the entire website. It is often set during development so that Google does not index an unfinished site, and then forgotten at launch. The website is finished but not on Google. In WordPress, a ticked "Discourage search engines from indexing this site" option does the same. So after launching a new website, check robots.txt first. Regular checks as part of website maintenance will also catch this kind of mistake.