How to Write a robots.txt File
Create a robots.txt that lets search engines crawl your site, keeps them out of the pages you don't want indexed, and points them to your sitemap.
robots.txt is a plain text file that tells search engine crawlers which parts of your site they may visit. It is the first thing Googlebot requests. A correct one takes five lines.
Create the file at your site root
The file must live at the root of your domain and be named exactly robots.txt, all lowercase:
https://yourdomain.com/robots.txt
Crawlers only look there. A robots.txt in a subfolder does nothing.
Allow crawling
The minimum file that lets every crawler visit every page:
User-agent: *
Allow: /
User-agent: * means the rules apply to all crawlers. Allow: / means the whole site is open. If you have nothing to block, this is a complete file.
Block what should not be indexed
Add Disallow lines for paths crawlers should skip. Common ones are admin panels, search result pages, and internal API routes:
User-agent: *
Allow: /
Disallow: /admin/
Disallow: /api/
Disallow: /search
Paths are prefixes, so /admin/ blocks everything under it.
One warning: robots.txt is a request, not a lock. Well-behaved crawlers honor it, but it does not hide anything from people. Never rely on it for private content. Use authentication for that.
Point to your sitemap
Add one line with the full, absolute URL of your sitemap:
Sitemap: https://yourdomain.com/sitemap.xml
This is the single most useful line in the file. It tells crawlers exactly which pages exist so they do not have to discover them by following links.
Put it together
A complete, production-ready robots.txt:
User-agent: *
Allow: /
Disallow: /admin/
Disallow: /api/
Sitemap: https://yourdomain.com/sitemap.xml
Test it
Open https://yourdomain.com/robots.txt in a browser. You should see the plain text, not a 404 or an HTML page.
Then in Google Search Console, open Settings, then the robots.txt report. It shows whether Google fetched the file successfully and flags any lines it could not parse.
More Websites how-tos
- How to Add a Favicon to a Website
- How to Add a Website to Bing Webmaster Tools
- How to Add a Website to Google Search Console
- How to Add Alt Text to Images
- How to Add Google Analytics to a Website
- How to Add Open Graph Tags to a Website
- How to Add Structured Data to a Web Page
- How to Check DNS Propagation
- How to Check If a Website Is Indexed by Google
- How to Compress Images for a Website
- How to Connect a Domain to Vercel
- How to Create a Sitemap for a Website
- How to Make a Website Mobile-Friendly
- How to Move a Domain to Cloudflare DNS
- How to Redirect www to non-www
- How to Set a Canonical URL
- How to Set Up a 301 Redirect
- How to Set Up Email Forwarding for a Domain
- How to Write a Meta Description