Google CSE has indexed robots.txt
and now if someone searches for 'txt' it returns the robots.txt
file which is really not ideal (as this is a bog-standard Drupal site, the string robots.txt
actually appears in the text). How can I avoid this? Is there a setting somewhere in Google or should I add /robots.txt
to erm, robots.txt
or...?
1 Answer
You could add this to robots.txt:
Disallow: /robots.txt
In What if robots.txt disallows itself? Google's John Mueller says:
The only thing this would affect is if a link were pointing to the robots.txt and Google would otherwise index the content of the robots.txt file. That wouldn't be possible when it's disallowed by robots.txt.
So it seems that adding a disallow rule in robots.txt for robots.txt itself can help prevent robots.txt from getting indexed without preventing Googlobot from fetching the file to see what else is disallowed.
Another way to handle it would be to add a HTTP header to robots.txt that prevents indexing. This would be a similar solution to the problem Prevent XML sitemaps from showing up in Google search results. You would want the following HTTP header served for robots.txt:
X-Robots-Tag: noindex
Under Apache you would implement it with this .htaccess code:
<Files ~ "robots\.txt$">
Header append X-Robots-Tag "noindex"
</Files>
-
2This is great information, it's very hard to google for this. One important note:
Header
is not always available, it's frommod_headers
which for example Ubuntu doesn't load by default.– chxSep 6, 2018 at 13:02 -
1Yes, you might have to enable the module with
sudo a2enmod mod_headers
Sep 6, 2018 at 13:24 -
1Another hack that would work, I'm reluctant to even suggest this since it's so hacky, is to serve a HTML page, with a robots noindex meta tag, around the robots.txt directives. Search engines generally ignore invalid robots.txt directives, so you could add a simple HTML boilerplate on top, and still have the robots.txt directives in the file. This might throw other tools off, and I wouldn't recommend it for general use, but if you can't serve an x-robots-tag header and really, really need the URL gone ... Sep 12, 2018 at 12:31