How to stop XML sitemap from appearing in Google search results

I use the All in One SEO Pack Plugin to generate my sitemap.xml file. For some reason, google decided to place the actual sitemap.xml in the search results instead of just the included urls.

After searching on google, I found suggestions to add the following to my .htaccess file.

<FilesMatch “sitemap.xml”>
Header set X-Robots-Tag “noindex”

<Files sitemap.xml>
Header set X-Robots-Tag “noindex”

Neither worked and I figured out that they wouldn’t since those are virtual files and the plugin generates them when the sitemap.xml is requested.
Instead, I opened wp-content/plugins/all-in-one-seo-pack/aioseop_sitemap.php and I looked for the following line:

echo ‘<urlset xmlns="">’ . "\r\n";

I added the following line just after it then saved the file and uploaded it.

header("X-Robots-Tag: noindex", true);

This adds a x-robots-tag noindex http header to sitemap.xml requests and google doesn’t index the file. To verify, try opening your through web-sniffer and look for the X-Robots-Tag:noindex http response header. 🙂

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.