How to edit robots.txt in WordPress, Yoast, Rank Math and Cloudflare
In WordPress, the robots.txt file people see at yourdomain/robots.txt can come from four places: WordPress core, an SEO plugin, a real file in the site root, or a CDN such as Cloudflare adding its own lines on top. Edit the wrong one and nothing changes, which is why so many owners save a new rule in a plugin and then find the live file exactly as it was. This guide shows how to work out what is serving your file, how to edit it in Yoast SEO, in Rank Math, by SFTP or with a small code filter, how Cloudflare can add to it, which rules a typical WordPress site actually needs, and how to clear caches and confirm that Google has fetched the new version.
- Where a WordPress robots.txt comes from
- How to find out what is serving your file
- Editing it in Yoast SEO and Rank Math
- Editing the file directly
- How Cloudflare can change robots.txt
- A sensible WordPress robots.txt
- A worked example
- The fix, step by step
- Edge cases worth knowing
- Keeping it fixed
- Questions and answers
Where a WordPress robots.txt comes from
WordPress does not ship with a robots.txt file. When a request for /robots.txt reaches WordPress and no real file exists, WordPress builds one on the fly. This is the virtual robots.txt. Plugins can change the virtual file through a filter, and a CDN can change the response again on its way to the visitor. The order of precedence is what matters:
| Source | Where it lives | When it wins |
|---|---|---|
| Physical file | robots.txt in the site root, next to wp-config.php | Always, if it exists. The web server serves it before WordPress runs. |
| SEO plugin rules | Saved in the database and added to the virtual file | Only when there is no physical file |
| WordPress core | Generated by WordPress | When there is no physical file and nothing else changes it |
| Cloudflare additions | Cloudflare's edge | Added on top of whichever version the origin sends, if the setting is on |
The default virtual file in current WordPress looks like this, with the Sitemap line pointing at the core sitemap unless an SEO plugin replaces it with its own:
User-agent: *
Disallow: /wp-admin/
Allow: /wp-admin/admin-ajax.php
Sitemap: https://www.example.com/wp-sitemap.xml
/robots.txt loads, WordPress or a plugin is generating it.How to find out what is serving your file
Open https://www.example.com/robots.txt in a private window, then check from the command line so caches in your browser do not mislead you:
curl -s https://www.example.com/robots.txt
curl -sI https://www.example.com/robots.txt | grep -iE "^(server|cf-cache-status|age|last-modified|x-powered-by)"
Clues in the headers:
last-modifiedusually appears when the web server sends a real file, and often not when PHP generates it.x-powered-by: PHPor WordPress-style headers suggest the virtual file.cf-cache-status: HITwith a largeagemeans Cloudflare is serving a cached copy, which may be older than your latest edit.
If you have shell access, the definitive check is whether the file exists in the root:
ls -l /var/www/vhosts/example.com/httpdocs/robots.txt
That path is the Plesk default; on other hosts it is the folder that contains wp-config.php. Compare the file's contents with what the site serves. If they match, the physical file is in charge. If they differ, something between the file and the visitor is changing it, most often a CDN or a cache. If you see rules you did not expect, the robots.txt blocks guide explains how to tell which ones are doing harm.
Editing it in Yoast SEO and Rank Math
Yoast SEO
Go to Yoast SEO, then Tools, then File editor. If no robots.txt exists, Yoast offers a button to create one; it writes a real file to the site root, which then overrides the virtual file. Edit the text and save. If the File editor option is missing, file editing has been switched off on the site, usually by define( 'DISALLOW_FILE_EDIT', true ); in wp-config.php or by your host, or the folder is not writable. In that case upload the file by SFTP instead. Leave the protection in place; it exists for good security reasons.
Rank Math
Rank Math's editor is under Rank Math, then General Settings, then Edit robots.txt, and the menu item may only show in Advanced Mode. Rank Math does not write a file. It stores your text and serves it as the virtual robots.txt. That has one consequence that catches many people: if a physical robots.txt exists, the box is greyed out and Rank Math shows a notice. Rename or delete the physical file by SFTP and the editor unlocks.
Using a code filter instead
Without an SEO plugin, you can add to the virtual file from a small plugin or your child theme's functions.php:
add_filter( 'robots_txt', function ( $output, $public ) {
$output .= "Disallow: /?s=\n";
$output .= "Disallow: /search/\n";
return $output;
}, 20, 2 );
This only affects the virtual file, so it does nothing if a physical file exists. Use one method only. A physical file, a plugin and a filter all trying to set the same file is the usual reason for confusion. For the wider WordPress picture, including sitemaps, see robots.txt and sitemaps in WordPress.
Editing the file directly
Without a plugin, create a plain text file named robots.txt, all lower case, in the site root. Use a text editor rather than a word processor so no formatting is added. Save as UTF-8, with one rule per line. Upload it by SFTP or through your hosting file manager; in Plesk that is Files, then the httpdocs folder for the main domain.
On the server, editing in place avoids a download and re-upload:
sudo nano /var/www/vhosts/example.com/httpdocs/robots.txt
sudo chown siteuser:psacln /var/www/vhosts/example.com/httpdocs/robots.txt
sudo chmod 644 /var/www/vhosts/example.com/httpdocs/robots.txt
Replace siteuser with the subscription's system user. The file must be readable by the web server, or it returns 403, which Google treats as no rules. On nginx, check that /robots.txt is allowed to fall through to WordPress when no file exists. Some configurations serve it only as a static file, so the virtual file returns 404 and plugin edits never appear:
location = /robots.txt {
try_files $uri $uri/ /index.php?$args;
access_log off;
}
How Cloudflare can change robots.txt
Cloudflare can do two things to the file. The first is caching: plain text files are among the types Cloudflare caches by default, so after an edit the edge may keep serving the old copy until it expires or you purge it. In the dashboard, go to Caching, then Configuration, and use Custom Purge to purge the exact address https://www.example.com/robots.txt, plus the non-www version if it serves its own file.
The second is adding content. Cloudflare offers a managed robots.txt option, found with its bot and AI crawler settings, which adds Cloudflare's own lines to your file, such as rules telling known AI training crawlers not to crawl. If your live file contains lines you never wrote, especially near the top, check that setting before searching WordPress for them. Turn it off if you prefer to manage every line yourself, or keep it and write your own rules around it.
Remember that robots.txt is a request, not a lock. Well-behaved crawlers follow it; others ignore it. To actually stop unwanted bots, use firewall rules, and to decide which AI crawlers to allow, see fixing robots.txt AI blocks.
A sensible WordPress robots.txt
Most WordPress sites need very little. This is a good starting point:
User-agent: *
Disallow: /wp-admin/
Allow: /wp-admin/admin-ajax.php
Disallow: /?s=
Disallow: /search/
Sitemap: https://www.example.com/sitemap_index.xml
The search lines stop crawlers wasting time on internal search results, which can produce endless thin URLs. Use the sitemap address your setup actually serves: /sitemap_index.xml for Yoast and Rank Math, /wp-sitemap.xml for WordPress core.
/wp-content/ or /wp-includes/. Search engines need the CSS, JavaScript and images in those folders to render your pages. Blocking them can make pages look broken to Google and harm how they are assessed.Never use robots.txt to remove a page from Google. It stops crawling, not indexing, and a blocked page can still appear in results with no description. To keep a page out, leave it crawlable and add noindex, as the noindex and disallow guide explains.
A worked example
An owner manages robots.txt in Rank Math. After restoring the site from a backup, they want to add Disallow: /?s=, but the live file no longer matches anything in Rank Math, and the editor box is now greyed out. Clearing the browser cache and purging the caching plugin make no difference.
Listing the site root by SFTP shows an old robots.txt written years earlier by a plugin that has since been removed, brought back by the backup. The web server serves that file before WordPress ever runs, so Rank Math's virtual version never reaches anyone. The owner renames the old file to robots-old.txt, purges /robots.txt in Cloudflare, and reloads. Rank Math's version appears at once, including the new rule.
The lesson is to check the site root after any restore or migration. A stray physical file is invisible from the WordPress dashboard, yet it decides what every crawler reads.
The fix, step by step
Step 1
Load the live file and check its source
Open /robots.txt in a private window and with curl -s, then list the site root by SFTP or the hosting file manager. If a robots.txt file exists in the root, that file is what visitors get, and plugin settings are ignored. If no file exists, WordPress or a plugin is generating it. Check the cf-cache-status header too. Write down which source is live before editing anything, so you change the right one first time.
Step 2
Choose one place to manage it
Decide whether you will manage robots.txt through a plugin or as a physical file, and remove the other. If you want to use Rank Math or a code filter, rename an old physical file to robots-old.txt so the virtual file can show. If you prefer a physical file, leave the plugin's robots settings alone. One source of truth prevents the most common problem, which is edits that never appear on the live site.
Step 3
Edit in Yoast SEO
In Yoast SEO, open Tools, then File editor. Create the file if Yoast offers to, edit the rules and save. Yoast writes a physical file to the site root. If the File editor option is missing, file editing is disabled by DISALLOW_FILE_EDIT or the root is not writable, so upload the file by SFTP instead. Reload /robots.txt to confirm the saved text appears exactly as you typed it, with no leftover rules.
Step 4
Edit in Rank Math
In Rank Math, switch to Advanced Mode if needed, then open General Settings and Edit robots.txt. Type the rules and save. Rank Math serves them as the virtual file, so the box is locked when a physical robots.txt exists; rename that file first. Reload /robots.txt and check that Rank Math's text appears. If nothing changes on nginx, check the location = /robots.txt block passes requests to index.php when no file exists.
Step 5
Or edit the physical file
Without a plugin, create a plain UTF-8 text file called robots.txt in the folder that holds wp-config.php, with one rule per line. Edit it in place on the server with sudo nano, or upload it by SFTP or the file manager. Set it to 644 and owned by the site user, so the web server can read it. Load the file in a browser and confirm the status is 200 and the content is yours.
Step 6
Check Cloudflare's settings
If the live file contains lines you did not write, open the Cloudflare dashboard and look at the managed robots.txt option among the bot and AI crawler settings. Turn it off if you want full control, or keep it and add your rules around it. Then compare the file served through Cloudflare with the file on the origin. When they match apart from the managed lines, Cloudflare is behaving as intended.
Step 7
Purge every cache
Purge /robots.txt in your caching plugin, in any server cache, and in Cloudflare using Custom Purge with the exact address. Purge each host that serves its own copy, such as the www and non-www versions. Then run curl -sI and check that cf-cache-status shows a miss or the age has reset. Without this step, visitors and crawlers can keep receiving the old file for hours after your edit.
Step 8
Confirm Google has the new version
Open the robots.txt report under Settings in Google Search Console. Check the fetch date and status for your host, and request a recrawl if the date is from before your edit. Google generally refreshes its copy within a day anyway. Once the report shows a successful fetch after your change, the new rules are in effect for Google. Run URL Inspection on a page you meant to allow to confirm it is not blocked.
Edge cases worth knowing
- Multisite. All sites in a subdirectory network share one root, so one physical robots.txt applies to every site. A virtual file can differ per site.
- www and non-www. If both hosts serve content, each needs its own correct file. Usually one redirects to the other, and the file at the final address is the one that counts.
- Staging sites. A staging robots.txt with
Disallow: /copied to live is a common launch mistake. Check the file on launch day. - Search engine visibility. The Discourage search engines box under Settings, then Reading, adds a
noindexto pages. It should be unticked on a live site. - Caching plugins. Some page caches store
/robots.txtlike any page. Purge it, or exclude it from caching, after each change.
Keeping it fixed
Pick one source and stick with it: a plugin's virtual file or a physical file, not both. Write down which one you use somewhere your team will find it. After every change, load the live file with curl, purge the CDN copy, and check the robots.txt report under Settings in Search Console, which shows when Google last fetched the file and whether the fetch worked. If the file ever fails to load, the robots.txt unreachable guide explains why that is urgent.
1. Where is the robots.txt file in WordPress?
By default there is no robots.txt file in WordPress; WordPress generates a virtual one whenever someone requests the address. That is why you will not find it by SFTP on a fresh site. If a physical file exists, it sits in the site root, the folder that contains wp-config.php, and it overrides the virtual file. SEO plugins either write that physical file, as Yoast does, or change the virtual file, as Rank Math does, depending on the plugin.
2. Why is my robots.txt not updating in WordPress?
The usual reason is that you edited one source while another is being served, most often a plugin setting when a physical robots.txt file in the site root overrides it. Caching is the second most common reason: a caching plugin, server cache or Cloudflare may still be serving the old copy. List the site root by SFTP to look for a physical file, purge the address in every cache layer, then reload the live file with curl to see what is really being served.
3. Why is the Rank Math robots.txt editor greyed out?
The Rank Math robots.txt editor is greyed out because a physical robots.txt file already exists in your site root. Rank Math edits the virtual file that WordPress generates, and the web server always serves a real file first, so Rank Math locks the box to avoid edits that would never appear. Rename or delete the physical file by SFTP or your hosting file manager, reload the settings page, and the editor becomes available.
4. Why does Cloudflare add text to my robots.txt?
Cloudflare adds text to robots.txt when its managed robots.txt option is switched on, which inserts Cloudflare's own lines, such as rules asking AI training crawlers not to crawl. These lines are added at the edge, so you will not find them in WordPress or on your server. Check the option among the bot and AI crawler settings in your Cloudflare dashboard, and turn it off if you want to write every line yourself, then purge the cached file.
5. Should I block wp-content in robots.txt?
No, you should not block wp-content or wp-includes in robots.txt. Those folders hold the stylesheets, scripts, fonts and images that search engines need to render your pages as visitors see them. Blocking them can make pages appear broken to Google and can affect how they are assessed. Older advice recommended blocking them, but that dates from before Google rendered pages fully. A sensible WordPress file blocks wp-admin, allows admin-ajax.php and lists your sitemap.
6. Can I use robots.txt to remove a page from Google?
No, robots.txt stops crawling, not indexing, so it is the wrong tool for removing a page. A page blocked in robots.txt can still appear in search results if other pages link to it, usually without a description. To remove a page, keep it crawlable and add a noindex robots meta tag, or delete it so it returns 404 or 410. For urgent cases, the Removals tool in Search Console hides a URL temporarily while the permanent fix takes effect.
7. What should a WordPress robots.txt contain?
A typical WordPress robots.txt needs only a few lines: a User-agent line for all crawlers, a Disallow for /wp-admin/, an Allow for /wp-admin/admin-ajax.php, and a Sitemap line with the full address of your sitemap. You can add Disallow lines for internal search results to stop crawlers wasting time on them. Avoid blocking theme or plugin folders, and add other rules only when you know a specific path should not be crawled.
The robots and sitemap checker on aiwebpageseo.com fetches your live robots.txt, shows every rule and Sitemap line it contains, and flags rules that block pages or files search engines need.
Open Robots and Sitemap Fixes โ