I have a simple php redirect script (link.php) that I use to keep track of our affiliate links. (Example: http://www.example.net/link.php?id=1 will bring you to http://www.product1url.com)
I've noticed that Google is indexing http://www.example.net/link.php?id=1. I have link.php set to noindex in Robots.txt but that's not stopping the indexing. So I want to add a "noindex", "nofollow" header to each URL itself.
Here's the script I have:
<?php
$path = array(
'1' => 'http://www.producturl1.com',
'2' => 'http://www.producturl2.com',
);
if (array_key_exists($_GET['id'], $path))
header('Location: ' . $path[$_GET['id']]);
?>
How do I modify this to include: "X-Robots-Tag: noindex, nofollow"? Is this possible?
You can output as many headers as you want as long as they are in the code before any potential output is generated. Generally the redirect should be last, though.
Simply add your header("X-Robots-Tag: noindex, nofollow", true);
before the $path = array(
line.
Also, I know this wasn't in the question, but you will want to update your sitemap.xml file for the index.php URI set to today's date. This will often lead to faster deindexing. (see: https://www.reddit.com/r/bigseo/comments/5nbh3n/google_ignoring_my_noindex_tags/ the post from johnmu, who is (was?) a Google employee.)