This guide explains how robots.txt works on a WordPress site and walks site owners and admins through checking and changing it safely. Robots.txt is a small text file at the root of your domain that tells search engine crawlers, the bots that fetch your pages, which addresses they may visit. On WordPress it is easy to lose track of. WordPress can generate the file on the fly, an SEO plugin can rewrite it, and a forgotten file on disk can override both. The short answer is to keep it short. Block WordPress admin and internal search results, leave your theme files, plugin files and images open, and use a noindex tag instead of robots.txt when you want a page out of Google. By the end you will know which file your site really serves, and you will have proof it says what you intended.
What you’ll accomplish
- You will know whether your robots.txt is a real file on disk or one WordPress generates.
- You will have a short, sensible rule set that blocks the right things and nothing more.
- You will understand why robots.txt controls crawling but cannot remove a page from search results.
- You will have an optional policy for AI crawlers such as GPTBot, written the way you intend.
- You will have confirmed the live file with
curland in the Google Search Console robots.txt report.
Prerequisites
You need a WordPress site you administer, and either SSH access or cPanel File Manager to look at its files. SSH is the secure remote terminal. The commands below also use WP-CLI, the official WordPress command line, which most cPanel hosts include. Run wp --info once you are logged in to check that it works.
Everything here applies to WordPress 5.5 and later, which added the built-in XML sitemap. You also need the site verified in Google Search Console for the last check. Setting up Google Search Console for WordPress covers that if you have not done it.
On cPanel, the file for your main domain lives in the account’s document root, usually ~/public_html. An addon domain has its own document root, which cPanel shows under Domains.
How it works
When a crawler asks for https://example.com/robots.txt, the web server first checks whether a file called robots.txt exists in the document root. If it does, the server sends it straight back and WordPress never runs. If it does not, the request falls through to WordPress, which builds a virtual file with a PHP function called do_robots(). The standard WordPress .htaccess rules make this choice. They only hand a request to WordPress when no real file matches.
On a default install with sitemaps enabled, the virtual file looks like this:
User-agent: *
Disallow: /wp-admin/
Allow: /wp-admin/admin-ajax.php
Sitemap: https://example.com/wp-sitemap.xml
User-agent: * means the rules apply to every crawler. Disallow: /wp-admin/ keeps bots out of the dashboard. The Allow line reopens admin-ajax.php, a file many themes and plugins call from the public side of the site. The Sitemap line points crawlers to the built-in XML sitemap. SEO plugins that replace that sitemap usually swap this line for their own, such as sitemap_index.xml.
Plugins and custom code change the virtual file through a hook called the robots_txt filter. SEO plugins handle robots.txt in two different ways, and the difference matters. Rank Math and All in One SEO (AIOSEO) edit the virtual output through that filter. Yoast SEO’s file editor, under Yoast SEO → Tools → File editor, writes a real robots.txt file to disk. Once a real file exists, every filter-based rule stops showing up, because WordPress is no longer asked. Menu names can differ between plugin versions.
The most important idea is the difference between crawling and indexing. Crawling is a bot fetching a page. Indexing is the search engine storing that page so it can appear in results. Robots.txt only controls crawling. Google can still list a blocked address if other sites link to it, usually with no description. To remove a page from results you need a noindex signal, and Google has to be allowed to crawl the page to see it.
Step-by-step
-
Look at the file your site serves today. From any terminal, fetch it with
curl. Replaceexample.comwith your domain.curl -s https://example.com/robots.txtOn an untouched WordPress site you should see the four-line default shown above. If you see something very different, such as
Disallow: /on its own underUser-agent: *, your whole site is closed to crawlers. Treat that as urgent and keep going. If you get an HTML page or a 404 error, WordPress is not answering the request; see Troubleshooting. -
Find out whether the file is physical or virtual. Log in over SSH and look in the document root.
ls -l ~/public_html/robots.txtIf a real file exists, you get a line such as
-rw-r--r-- 1 user user 182 Sep 22 10:14 /home/user/public_html/robots.txt. That file is what crawlers see, and plugin settings will not change it. If you getls: cannot access '/home/user/public_html/robots.txt': No such file or directory, the file is virtual and WordPress builds it.Headers give a hint from the outside too. Run
curl -sI https://example.com/robots.txt. A physical file usually comes back withLast-Modified,ETagandAccept-Ranges: bytesheaders. The virtual file usually showscontent-type: text/plain; charset=utf-8and none of those three. Thelscheck is the reliable one. -
Check the search engine visibility setting. WordPress has a checkbox under Settings → Reading called Discourage search engines from indexing this site. It is stored as the
blog_publicoption.wp option get blog_publicA public site prints
1. If it prints0, the box is ticked. Since WordPress 5.3 this setting no longer addsDisallow: /to robots.txt. Instead, every page carries a noindex tag, and the core sitemap and itsSitemapline disappear. Confirm what the pages say:curl -s https://example.com/ | grep -i "name='robots'"A public site usually prints
<meta name='robots' content='max-image-preview:large' />. A discouraged site prints<meta name='robots' content='noindex, nofollow' />. SEO plugins may print their own version of this tag with more values. If your live site showsnoindex, untick the box in Settings → Reading and save. -
See what WordPress would generate. Even when a physical file exists, you can print the virtual version to compare.
wp eval 'do_robots();'You get the same text format as step 1, including any rules your SEO plugin adds. If this output differs from what
curlreturned in step 1, a physical file or a layer in front of the site is winning. -
Decide what to block. Keep the rule set small. These additions are sensible for most WordPress sites:
Disallow: /?s=andDisallow: /search/keep bots out of internal search results. Every search term creates a new address for crawlers to waste time on.Disallow: /*?add-to-cart=stops bots from following WooCommerce “add to cart” links, which create cart sessions on your server. Skip it if you do not run WooCommerce.
Leave these open. Never disallow
/wp-content/or/wp-includes/. Google renders your pages the way a browser does, so it needs your theme’s CSS (stylesheets), JavaScript and images. Blocking them can make Google see a broken page and rank it lower. Do not use robots.txt to hide tag pages, category pages or thin content. Use your SEO plugin’s noindex setting for those, so Google can crawl the page and see the tag. -
Add the rules in one place only. Pick the method that matches step 2.
If the file is virtual and you use Rank Math or AIOSEO, add the lines in the plugin’s robots.txt editor. If you prefer code, create a must-use plugin, which is a PHP file WordPress loads automatically from
wp-content/mu-plugins/. Save this aswp-content/mu-plugins/robots-rules.php:<?php add_filter( 'robots_txt', function ( $output, $public ) { if ( '1' !== (string) $public ) { return $output; } $extra = "Disallow: /?s=\n"; $extra .= "Disallow: /search/\n"; return str_replace( "Allow: /wp-admin/admin-ajax.php\n", "Allow: /wp-admin/admin-ajax.php\n" . $extra, $output ); }, 10, 2 );The code inserts the new lines inside the
User-agent: *group, right after the admin rule, and does nothing on a discouraged site. If the file is physical, edit~/public_html/robots.txtdirectly in File Manager or over SSH instead. A filter will not reach it. -
Decide on AI crawlers (optional). AI companies run their own crawlers, and you can ask each one to stay away by name.
GPTBotis OpenAI’s crawler.CCBotbelongs to Common Crawl, a public web archive many AI models are trained on.Google-Extendedis a control token Google reads to decide whether your content may be used for its Gemini models. Blocking it does not affect Google Search. To block all three, add these lines. In the filter above, append them to$outputjust before thereturnline; in a physical file, add them at the bottom.User-agent: GPTBot Disallow: / User-agent: CCBot Disallow: / User-agent: Google-Extended Disallow: /Robots.txt is a request, not a lock. Well-behaved crawlers follow it, and others ignore it. If AI bots are actually loading your server, measuring AI crawler traffic on cPanel shows how to find them in your logs and block them at the server.
-
Handle staging sites differently. A staging site, the test copy you change before touching production, should be protected with a password, not only with robots.txt. Any link to it can still get it listed. Tick Discourage search engines on staging, and turn on password protection in cPanel’s Directory Privacy tool. When you push staging to production, check
wp option get blog_publicreturns1afterwards. A copied staging setting is one of the most common reasons a live site vanishes from Google.
Validate it works
Run the fetch again and compare it with what you intended.
curl -s https://example.com/robots.txt
You should see your new lines, for example Disallow: /?s= directly under Allow: /wp-admin/admin-ajax.php, and the Sitemap line at the end. If you added AI rules, each User-agent block appears once.
Then check Google’s view. In Google Search Console, open Settings, find robots.txt in the Crawling section, and open the report. It lists the robots.txt files Google found for your site, when it last fetched each one, and any lines it could not parse. Google usually caches robots.txt for up to a day. After an urgent fix, such as removing an accidental Disallow: /, use Request a recrawl from the menu next to the file.
Finally, use the URL Inspection tool on a normal post, which should report that crawling is allowed.
Troubleshooting
Plugin changes do not show up. A physical robots.txt in the document root is taking precedence. Copy anything you want to keep from it, then either edit the file directly or delete it so WordPress and the plugin take over. Run step 2 again to confirm which one is live.
/robots.txt returns a 404 or your homepage. The virtual file relies on the same rewrite rules as pretty permalinks. Under Settings → Permalinks, a site set to Plain may not route /robots.txt to WordPress. Choose any other structure and save. If WordPress lives in a subfolder such as /blog/, crawlers only read robots.txt at the domain root, so place a physical file there.
Google shows a page “Indexed, though blocked by robots.txt”. Google found links to an address it may not crawl, so it listed the address without reading it. If you want it gone, remove the Disallow rule for that address and add a noindex tag. Google has to crawl the page once more to see the tag. Finding accidental noindex headers on WordPress helps when the opposite happens and pages vanish unexpectedly.
Lines appear that nobody on your team wrote. Something in front of WordPress may be adding them. Compare curl -s https://example.com/robots.txt with wp eval 'do_robots();'. If the live file has extra AI-bot lines and your site uses Cloudflare, check its managed robots.txt setting in the Cloudflare dashboard.
The site dropped out of search after a migration. A staging robots.txt with Disallow: / or a blog_public value of 0 often travels with the files and database. Run steps 1 to 3 on the live site, fix what you find, then request a recrawl in Search Console.
When this is the wrong approach
Robots.txt is the wrong tool for anything private. The file is public, so listing a secret folder in it advertises the folder. Protect private areas with a login or a password, and treat robots.txt as a traffic sign for polite bots.
It is also the wrong tool for removing pages from Google. Use noindex, or delete the page and let it return a 404 or 410 status. For an urgent removal, Search Console has a Removals tool that hides a URL temporarily while the permanent fix takes effect.
Finally, robots.txt cannot slow down an aggressive bot. Google ignores the Crawl-delay line, and badly behaved scrapers ignore the whole file, so blocking at the server or firewall is the real fix. Google’s own introduction to robots.txt and the WordPress reference for do_robots() are good next reads if you want the full rules.
Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.