Search code examples
phpwordpress.htaccessurl

set urls with parameter to noindex in wordpress?


I'm looking for a solution to set all URLs which uses the parameter '?' to noindex. I look for a php solution which I could use in the header.php in Wordpress or in the .htaccess.

I tryed this:

<?php
$url = $_SERVER['REQUEST_URI'];
if( preg_match('/\?/', $url) ) {
    echo '<meta name="robots" content="noindex, follow" />' . "\n";
}
?>

This solution did not work and URLs with the parameter '?' didn't get a noindex attribute.

Best regards


Solution

  • You can use strpos() function for that:

    <?php
        $url = $_SERVER['REQUEST_URI'];
        if (strpos($url,'?') !== false) {
            echo '<meta name="robots" content="noindex, follow" />' . "\n";
        }
    ?>