PHP Conditional to noindex Matched URL

Sitebee

Freelance Technical SEO
Staff member
A simple quick and easy solution to deploy a noindex tag on specific matching URLs or parameters using a PHP If conditional. This will be useful if you have limted to no access to amend the robots.txt or you're looking at building a conditional logic for keeping URLs out of Google's Index. Such as faceted URLs, empty product categories, seasonal URLs, etc.

Add the following PHP snipped to your global HEAD file (the file that controls the meta tags).
Code:
<?php
$url = $_SERVER['REQUEST_URI'];
    if (strpos($url,'/URI-GOES-HERE/') !== false) {
    echo '<meta name="robots" content="noindex, nofollow" />' . "\n";
    }
?>

Before you add the PHP Conditional to noindex Matched URL, here are a few considerations. This method does not override any existing meta robots tags, it will add an additional tag if you have existing meta robots.

This method also assumes that you have a global (sitewide) file that controls meta data, for example WordPress has the header.php file. You can add regex rules for faceted URLs, etc.

You can also use Google Tag Manager to deploy a noindex tag, see my post for using GTM - https://sitebee.net/hub/threads/noindex-metarobots-urls-using-google-tag-manager.17/ (I would recommend that method if you do not know PHP basics).
 
Last edited: