Search code examples
subdomaincdngoogle-index

How to remove subdomain from google index, which links to the main domain


Can anyone tell me how can i remove the subdomain from google index, which links to the main domain.

Lets say my domain is www.myweb.com and my subdomain is cdn.myweb.com. Now here the Document Root of the subdomain is same as the main domain. So i could not use the robot.txt to stop google indexing, as it will remove indexing the main domain links too.

I search on google, bing and stackoverflow too, but i could not find the perfect answer for the question. Does any solve from yours side?


Solution

  • You can use dynamic robots.txt for this purpose. Something like this...

    httpd.conf (.htaccess):

    RewriteRule /robots\.txt$ /var/www/myweb/robots.php
    

    robots.php:

    <?php
    header('Content-type: text/plain');
    
    if($_SERVER['HTTP_HOST']=='cdn.myweb.com'){ 
        echo "User-agent: *\n";
        echo "Disallow: /\n";
    }else{              
        include("./robots.txt");    
    }