How to Create a robots.txt File (With Examples for WordPress and Static Sites)

A robots.txt file is a plain text file that sits at the root of your website and tells search engine crawlers which parts of the site they may and may not visit. It is one line per rule, it takes five minutes to write, and getting it wrong can hide your whole site from Google — which is why this guide shows you the exact syntax, five copy-paste examples, and the mistakes to avoid.

The first thing to understand is what robots.txt cannot do. It is a request, not a lock: well-behaved crawlers obey it, but it does not keep a page private. Anything you do not want anyone to see needs a password, not a robots.txt rule.

Close-up of HTML code displayed on a computer monitor

How to Create a robots.txt File: The Syntax, Line by Line

A robots.txt file has two kinds of lines: User-agent lines say which crawler a rule applies to, and Disallow (or Allow) lines say which paths that crawler may or may not visit. Rules are grouped: each User-agent line starts a new group, and the Disallow and Allow lines that follow it belong to that group.

User-agent: *
Disallow: /private/

That file says: every crawler (the * wildcard means all of them) must not visit anything under /private/. A few syntax rules matter:

  • Paths are case-sensitive. Disallow: /Private/ does not block /private/.
  • A trailing slash matters. Disallow: /blog blocks /blog, /blog-post, and /blog/anything. Disallow: /blog/ blocks only paths under the /blog/ directory.
  • The * wildcard matches any sequence of characters. Disallow: /*.pdf$ blocks all PDF files — the $ means “end of URL”, so it matches only URLs that end in .pdf.
  • The most specific rule wins. If a Disallow and an Allow both match a URL, the longer, more specific path takes priority. A Disallow: / and an Allow: /public/ together mean everything is blocked except /public/.
  • Comments start with #. Anything after a # on a line is ignored, so you can annotate your rules.

There is one more line worth knowing: Sitemap: — it tells crawlers where your sitemap lives. It is not part of any group, so it goes at the end of the file on its own. If you do not have a sitemap yet, our guide to creating a sitemap.xml walks through it.

5 Copy-Paste robots.txt Examples

Each example below is a complete file. Copy the one that fits your site, change the paths and the sitemap URL, and you are done.

1. Allow everything (the default for most sites)

User-agent: *
Disallow:

An empty Disallow means “nothing is blocked”. This is the right file for most small sites — you are explicitly telling crawlers they may visit everything. Add your sitemap line and you are finished.

2. Block a private directory

User-agent: *
Disallow: /private/
Disallow: /drafts/

Sitemap: https://example.com/sitemap.xml

Use this when you have folders that should not appear in search results — staging areas, internal pages, draft content. Remember: this hides them from search results, not from people. Anyone who knows the URL can still open the page.

3. Block search results and filter pages

User-agent: *
Disallow: /search?
Disallow: /*?sort=
Disallow: /*?filter=

Sitemap: https://example.com/sitemap.xml

Sites with search boxes and product filters can generate thousands of near-identical URLs. Blocking the query parameters keeps crawlers on your real pages instead of wasting their crawl budget on duplicates.

4. Block one crawler, allow the rest

User-agent: BadBot
Disallow: /

User-agent: *
Disallow:

Groups are separated by a blank line. This file blocks a crawler called BadBot from the whole site while letting every other crawler visit everything. Replace BadBot with the actual crawler’s user-agent string — and remember that a crawler that ignores robots.txt will ignore this too.

Computer screen with program code in a modern office workplace

5. Block AI training crawlers

User-agent: GPTBot
Disallow: /

User-agent: CCBot
Disallow: /

User-agent: *
Disallow:

GPTBot is OpenAI’s crawler and CCBot is Common Crawl’s. Blocking them stops those companies using your content for AI training. Note that this does not stop your content appearing in AI answers that draw on search results — that uses the search crawlers, which you want to allow.

How to Add robots.txt in WordPress

WordPress does not create a physical robots.txt file by default — it generates a virtual one on the fly. That virtual file is fine for most sites, but you cannot edit it directly, so you need a plugin or a real file.

With Rank Math (recommended): go to Rank Math > General Settings > Edit robots.txt. The editor shows your current file and lets you edit it directly. Save, and Rank Math serves the updated file. Rank Math also warns you if a rule would block your whole site.

With Yoast SEO: go to SEO > Tools > File Editor, and edit the robots.txt tab. Yoast will not show the editor if a physical robots.txt file already exists — delete the physical file first, or edit that instead.

As a physical file: create a file named robots.txt (lowercase, exactly that name) and upload it to the root of your site — the same folder that contains wp-config.php — using your host’s file manager or FTP. A physical file overrides the virtual one. This is the most reliable method, and it works regardless of which plugins you have installed.

Whichever method you use, check the result by visiting https://yoursite.com/robots.txt in your browser. You should see your file, exactly as you wrote it. If you see a 404, the file is in the wrong place.

How to Add robots.txt to a Static Site

For a static site — plain HTML, a Jekyll or Hugo site, anything deployed to Netlify, Vercel, or GitHub Pages — the process is simpler. Create a file named robots.txt in the folder that gets published as your site root (for most static generators, that is the folder that becomes the published site — often called “public”, “dist”, or “_site”). Deploy, then visit https://yoursite.com/robots.txt to confirm it is live.

One gotcha: some static hosts cache aggressively. If your new file does not appear, wait a few minutes and hard-refresh (Ctrl+Shift+R or Cmd+Shift+R). If it still does not appear, check that the file is in the published folder and not in a source folder that never gets deployed.

If you are building the file by hand and want to check the syntax, our robots.txt generator builds a valid file from your choices — pick the crawlers and the paths, and it writes the rules for you.

HTML and JavaScript code on a computer screen in Visual Studio Code

How to Test Your robots.txt File

Never trust a robots.txt file you have not tested. A single misplaced slash can block your entire site, and the error is silent — Google simply stops crawling, and you notice weeks later when your traffic drops.

Google Search Console’s robots.txt tester: open Search Console, go to Settings, and click “Open report” next to robots.txt. It shows the file Google last fetched and when. To test a specific URL, use the URL Inspection tool — it tells you whether Googlebot can fetch the page.

The manual check: open your robots.txt in a browser and read it line by line. Look for the two most common errors: a Disallow: / with no Allow to balance it, and a path typo that blocks a real directory.

After any change: ask Google to re-fetch the file. In Search Console, use the URL Inspection tool on your homepage and request indexing — that prompts a fresh crawl, and Google re-reads robots.txt as part of it.

Common robots.txt Mistakes That Block Your Whole Site

  • Disallow: / on a live site. This blocks everything. It is the right rule for a staging site and a disaster on a live one. If your site suddenly vanishes from search results, check this first.
  • Using robots.txt to hide private content. A disallowed URL can still appear in search results (without a description), and anyone can read your robots.txt file to find the paths you blocked. Private content needs authentication, not a robots rule.
  • Blocking CSS and JavaScript. Google needs your CSS and JS to render pages properly. Old robots.txt files often blocked /wp-includes/ or asset folders — do not do that. Let Google fetch your styles and scripts.
  • Case mistakes. Disallow: /Images/ does not block /images/. Paths in robots.txt are case-sensitive, and a wrong case means the rule silently does nothing.
  • Forgetting the file is public. Your robots.txt is visible to anyone at yoursite.com/robots.txt. Do not put sensitive paths in it — you are advertising them.

If you want a quick way to check what your site is telling crawlers, our sitemap generator pairs with this file: the sitemap lists the pages you want crawled, and robots.txt lists the ones you do not. Together they are the two files that define how search engines see your site.

Laptop screen displaying code on a desk

How to Create a robots.txt File: Frequently Asked Questions

Do I need a robots.txt file?

Strictly, no — a site without one is crawled normally, as if every page were allowed. But having one is still worth it: it lets you point crawlers to your sitemap, block junk URLs, and make your crawl rules explicit instead of accidental.

Where does robots.txt go?

At the root of your domain: https://yoursite.com/robots.txt. A robots.txt file in a subdirectory (yoursite.com/blog/robots.txt) is ignored by crawlers — only the root file counts.

Does robots.txt stop Google indexing a page?

Not reliably. A disallowed page can still appear in Google’s index, usually without a description, because Google may learn about it from links on other pages. To keep a page out of the index, use a noindex meta tag instead — and do not block it in robots.txt, or Google cannot fetch the page to see the noindex tag.

How long does it take Google to respect a robots.txt change?

Google re-fetches robots.txt regularly, usually within a day. After a change, you can speed things up by requesting indexing on your homepage in Search Console, which prompts a fresh crawl.

1 thought on “How to Create a robots.txt File (With Examples for WordPress and Static Sites)”

  1. Pingback: How to Create a Sitemap.xml for Your Website (2026)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top