Keeping a staging site out of Google
Use a password, or a noindex instruction, or both. A robots.txt file alone won't do the job, and Google's own help pages say so.
A test copy that shows up in search results is a real problem. People land on a half-finished page, and your live pages compete with a near-identical twin.
Why robots.txt alone isn't enough
A robots.txt file is a plain text file that asks crawlers to stay away from certain addresses. Crawlers are the programs search engines use to visit pages. Google's introduction says it is "not a mechanism for keeping a web page out of Google"1. To keep a page out, it points you to noindex or to "password-protect the page"1.
Here's what goes wrong. If another site links to a blocked page, the address can still be listed. Google says a page blocked this way can show up in results, but the result "won't have a description"1. You'd see the address with nothing written under it.
Then there's the honour system. Google says it's up "to the crawler to obey them", and some crawlers don't1. So robots.txt isn't protection against anyone with bad intent either.
What noindex does
Noindex is an instruction placed on the page, either as a meta tag in its code or as a response header the server sends. Once Google crawls the page and reads the instruction, it "will drop that page entirely from Google Search results"2.
A header can cover files that have no page code. Google notes "A response header can be used for non-HTML resources" such as PDFs and images2.
Now the catch that trips people up. Noindex only works if Google can see it. The page "must not be blocked by a robots.txt file"2. If it is, Google never reads the instruction, and "the page can still appear in search results"2.
So don't stack robots.txt blocking on top of noindex. Pick noindex, or add the password, and leave robots.txt alone for the test copy.
The tick-box in WordPress
If you use WordPress, the Reading settings screen has a box labelled "Discourage search engines from indexing this site"3. Ticking it is meant "to ask search engines not to index this site"3.
It's a request, not a lock. The documentation says "it is up to search engines to honor your request"3. Treat it as one layer.
Watch out when you move changes. If the tick-box was on in staging and the settings travel to live, your real site could disappear from results. Check the setting on the live site afterwards. The guide to moving changes to live covers the push itself.
What hosts do by default
Some hosts switch this on for you. One says "By default, indexing is turned off for Staging sites", using a "combination of a WordPress setting and an HTTP header that we automatically add"4. Another static-site host says "By default, every preview deployment generated by Cloudflare Pages includes the X-Robots-Tag: noindex HTTP response header"5.
Defaults can differ by address. The first host says "These headers cannot be removed from Kinsta temporary URLs or staging URLs"4. A custom domain is its route if you need them gone4. So after any domain change, check again.
The password layer
A password stops everyone, including crawlers, because they can't get past the login. Google lists it as a way to keep a page out of Google1. Your host's help pages will show how to add one. Look for "password protect" or "restrict access" in the documentation.
One preview host notes its preview links are public until you change a setting. It says "By default, these deployment URLs are public"5. So read your host's defaults rather than assuming.
Checking it worked
- Open the copy in a private browser window. Do you hit a login, or see the page?
- View the page source and look for a robots meta tag.
- Search Google for your staging address a few weeks later. If it's listed, review the steps above.
Hidden from search doesn't mean hidden from everyone, so keep real customer data off the copy where you can.
Notes and sources
- Introduction to robots.txt, Google Search Central
- Block Search indexing with noindex, Google Search Central
- Settings Reading Screen, WordPress.org Documentation
- Staging environments, Kinsta Docs
- Preview deployments, Cloudflare Pages docs