phpindexingmeta-tagsheadnoindex

How to use noindex when my <head> is included in all pages with php include?


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';?>

Solution

  • 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';
    }
    ?>