Robots.txt Explained: What It Does and How to Write One
A robots.txt file is a small text file in the root of your website that tells crawlers which areas they may or may not visit. It follows the Robots Exclusion Protocol, which major search engines respect.
Where it lives
The file must be at the root of the domain, for example https://example.com/robots.txt. Each subdomain needs its own file. Search engines request it before crawling the rest of the site.
The basic syntax
User-agent: *
Disallow: /admin/
Allow: /admin/public/
Sitemap: https://example.com/sitemap.xml
User-agentnames the crawler the rules apply to.*means all crawlers.Disallowlists paths the crawler should not request.Allowmakes an exception inside a disallowed path.Sitemaptells crawlers where your XML sitemap is.
An empty Disallow: means nothing is blocked.
What robots.txt is good for
- Keeping crawlers out of low-value sections such as internal search results, carts or admin areas.
- Reducing crawl load on very large sites.
- Pointing crawlers to your sitemap.
What it is not
Robots.txt is not a security tool and it is not a reliable way to keep a page out of search results. The file is public, so listing a private path actually advertises it. A blocked URL can still appear in results without a description if other pages link to it. To keep a page out of the index, use a noindex meta tag or password protection, and let crawlers fetch the page so they can see the tag.
Common mistakes
- Blocking everything by accident.
Disallow: /underUser-agent: *blocks the whole site. This often happens when a staging file is copied to the live site. - Blocking CSS and JavaScript. Google needs them to render your pages properly.
- Using it to remove pages from Google. Use noindex or the removals tool instead.
- Typos in paths. Paths are case sensitive.
Create and test yours
Use the robots.txt generator to build a valid file, upload it to the root of your site and check it in Google Search Console. When you change it, request a recrawl so the update is picked up faster.