What a robots.txt file does and why you need one

A robots.txt file is a text file you place in your website's root directory to tell search engine crawlers which pages they can and cannot visit. Search engines like Google, Bing, and others read this file first when they arrive at your site, and they follow the instructions you write there. Without one, crawlers will visit every page they can find. With one, you can block them from wasting time on pages you don't want indexed, like admin panels, duplicate content, or private user areas.

The file is not a security tool — anyone can read it by typing your-domain.com/robots.txt into their browser. It is a courtesy instruction that most legitimate crawlers respect. Malicious bots often ignore it entirely, so never rely on robots.txt to hide sensitive information. Its real purpose is to reduce server load and keep your search results clean by preventing indexing of pages that don't belong in search.

Key Takeaways

  • A robots.txt file is a plain text file placed in your website's root directory that tells search crawlers which pages to visit and which to skip.
  • The file uses simple syntax: User-agent (which crawler), Disallow (which paths to block), and Allow (which paths to permit), followed by a Sitemap line pointing to your sitemap.
  • You can block all crawlers from your entire site, block specific crawlers from specific folders, or allow most crawlers while blocking one or two.
  • After creating the file, test it using Google Search Console or Bing Webmaster Tools to confirm crawlers will follow your rules.
  • The file must be named exactly robots.txt in lowercase and placed directly in your root directory, not in a subfolder.

The basic syntax and what each line means

A robots.txt file uses four main directives. User-agent specifies which crawler the rule applies to — use an asterisk (*) to mean all crawlers, or name a specific one like Googlebot or Bingbot. Disallow lists the paths you want to block, starting with a forward slash. Allow (optional) overrides a Disallow for specific paths — useful when you want to block a folder but allow certain pages inside it. Sitemap points to your XML sitemap so crawlers know where to find your important pages.

Here is a simple example that blocks all crawlers from your admin folder:

User-agent: * Disallow: /admin/ Sitemap: https://your-domain.com/sitemap.xml

This example blocks Googlebot from your /private/ folder but allows all other crawlers:

User-agent: Googlebot Disallow: /private/ User-agent: * Disallow: Sitemap: https://your-domain.com/sitemap.xml

The second User-agent block with an empty Disallow means "allow everything" for all other crawlers. You can stack multiple User-agent blocks to create different rules for different crawlers.

Where to place the file and how to create it

The robots.txt file must live in your root directory — the top level of your website where your home page lives. If your site is at example.com, the file goes at example.com/robots.txt. If your site is at example.com/blog/, the file still goes at example.com/robots.txt, not at example.com/blog/robots.txt. Search crawlers always look in the root first, and a file anywhere else will be ignored.

To create the file, open a plain text editor like Notepad (Windows), TextEdit (Mac), or any code editor. Do not use Word or Google Docs — they add formatting that will break the file. Type your rules, then save the file with the name robots.txt in lowercase. Upload it to your root directory using FTP, SFTP, your hosting control panel, or your site builder's file manager. If you use WordPress, Wix, Squarespace, or another platform, check your settings panel first — many let you edit robots.txt directly without uploading a file.

After uploading, visit your-domain.com/robots.txt in your browser to confirm the file is there and readable. You should see your rules displayed as plain text.

Common rules for different situations

If you want to block all crawlers from your entire site (useful for staging or development sites), use this:

User-agent: * Disallow: /

If you want to block crawlers from multiple folders, list each one:

User-agent: * Disallow: /admin/ Disallow: /private/ Disallow: /temp/ Sitemap: https://your-domain.com/sitemap.xml

If you want to block everything except a specific folder (like allowing /public/ but blocking everything else), use Allow to override:

User-agent: * Disallow: / Allow: /public/ Sitemap: https://your-domain.com/sitemap.xml

If you want to block specific file types like PDFs or images from being indexed, use a wildcard:

User-agent: * Disallow: /*.pdf Disallow: /*.jpg Sitemap: https://your-domain.com/sitemap.xml

Testing your robots.txt file

After you create and upload your file, test it to make sure crawlers will follow your rules. Google Search Console has a robots.txt tester built in — sign in, go to Settings, then robots.txt Tester, and paste your rules or let it read the file from your site. The tool will highlight any syntax errors and show you which paths are blocked and which are allowed.

Bing Webmaster Tools also has a robots.txt validator. Upload your site, go to Crawl Control, then robots.txt, and you can test specific URLs to see whether Bingbot would be allowed to crawl them. Both tools catch common mistakes like typos, incorrect paths, or rules that block everything by accident.

You can also manually test by typing a blocked path into your browser — if the page loads, your robots.txt is working (crawlers are blocked, but browsers are not). If you see a 404 error, the path does not exist on your site, so there is nothing to block anyway.

What happens if you don't have a robots.txt file

If you do not create a robots.txt file, search crawlers will visit every page they can find and index them all. This is fine for most websites — it means your content gets discovered. However, it can cause problems if you have duplicate pages, staging sites, admin panels, or user account pages that should not appear in search results. Crawlers will waste time and server resources on pages that don't belong in search.

You also lose the chance to point crawlers to your sitemap, which helps them find all your important pages faster. A sitemap line in robots.txt is optional but recommended because it makes crawling more efficient.

Frequently Asked Questions

Will robots.txt stop hackers from accessing my site?

No. robots.txt is a courtesy instruction for search crawlers, not a security tool. Anyone can read it by visiting your-domain.com/robots.txt, and malicious bots ignore it entirely. Never use robots.txt to hide passwords, API keys, or sensitive files. Use proper authentication, firewalls, and HTTPS encryption instead.

Do I need a robots.txt file if I have a sitemap?

No, but having both is better. A sitemap tells crawlers which pages exist and how often they change. A robots.txt tells crawlers which pages to skip. Together, they give crawlers a complete picture of your site. If you only have a sitemap, crawlers will still visit pages you want blocked.

What if I block a page in robots.txt but it still shows up in Google search results?

Google may have already indexed the page before you added the block, and it can take weeks for the index to update. Go to Google Search Console, find the page under Coverage, and request removal. You can also add a noindex meta tag to the page itself for faster results. The robots.txt block prevents future crawling, but it does not remove pages already indexed.

Can I use robots.txt to block a specific user or IP address?

No. robots.txt only recognizes User-agent names (crawler names) and paths. To block specific IPs or users, use your web server's .htaccess file (Apache), firewall rules, or your hosting control panel. robots.txt cannot distinguish between different visitors.

Does the order of rules in robots.txt matter?

Yes. Crawlers read rules from top to bottom and stop at the first match. If you have Allow and Disallow rules for the same path, put the more specific rule first. For example, if you want to block /admin/ but allow /admin/public/, list the Allow rule before the Disallow rule so the crawler matches the specific path first.