wordpresshttp-redirectx-robots-tag

How to add x-robots-tag for certain url pattern


i need help about adding x-robots-tag to certain url pattern.

My web site have many ?nonamp=1 ?amp kind of pattern that getting crawled by search engines. I would like to let engines crawl those urls but not want them to index these ones. (i couldn't add php no-index header for these certain url patterns)

My question is how can i add x-robots-tag to those urls pattern on my web server.

Web server: openlitespeed
Site: php (Wordpress)

Sample URLS:

example.com/amazon-nedir-amazonda-satis-nasil-yapilir/?amp=1

/amazon-nedir-amazonda-satis-nasil-yapilir/?nonamp=1

/amazon-nedir-amazonda-satis-nasil-yapilir/?amp

I would like to add x-robots-tag to all these 3 end pattern (?amp=1, ?nonamp=1, ?amp)

Thanks for help. Best Regards.


Solution

  • In functions.php add those lines of code:

    function add_header_xrobots($headers) {
        if (isset($_GET['amp']) || isset($_GET['noamp'])){
            $headers['X-Robots-Tag'] = 'noindex';
        }
        return $headers;     
    }
    add_filter('wp_headers', 'add_header_xrobots');
    

    Edit noindex to right header content.