0

I need to use <meta name="robots" content="noindex"> in my "thank you page".

Google instructions says that I need to put it between <head> tags but my head tags are shared (php included) from same file for all pages, even for those pages which I want to be indexed.

What is the right practice for this?

This is how I am including it on all pages (basic method), <head> tags are inside head.php file:

<?php include 'head.php';?>

1 Answers1

1

What I usually do with generic heads is that I declare some PHP variables before including the head file. These can be unique to each individual page, and so the meta names etc. can be changed depending on the page.

Example:

<?php
$metaName='robots';
$metaContent='noindex';    

include_once('head.php');
?>

Then in your head file:

<head>
    <meta name="<?php echo $metaName; ?>" content="<?php echo $metaContent; ?>">

    //What else you may have
</head>

Then depending on what you want, you can always restructure your logic, add and make use of more variables etc., but this should be more than enough to give you the general idea.

In case you don't want to set your $metaName and $metaContent variables on every page, you can choose to give them some default values in your head file.

Example:

<?php
if(!$metaName) {
    $metaName='default value';
}

if(!$metaContent) {
    $metaContent='default value';
}
?>
Martin
  • 2,326
  • 1
  • 12
  • 22
  • Ok thank you. I have one more question. Does this mean that I must now set that two variables on every page? Because now that head.php file will be "calling" them? For example: in my index.php which I want to be indexed do I need to set $metaContent='index'; or can I just leave it without this variable and it will do no harm? Do I need to set that 2 variables only on thank you page? – PeterVonSkala Sep 20 '18 at 09:11
  • In theory, yes, but you can just add default values to them if you wish in your head file. I'll edit my question to include that. – Martin Sep 20 '18 at 10:19
  • I set default values in head file but now I am getting: Undefined variable: metaName (and MetaContent) in (head file)... – PeterVonSkala Sep 22 '18 at 13:37
  • Are you including it properly? You need the variables defined *BEFORE* the head include. Then in your head you check if they exist or not, and if they don't you set their default value as I did in the example. Make sure you have no typos etc. If you still get the error, edit the code into your question so I can look at it @PeterVonSkala – Martin Sep 23 '18 at 15:26