Good day! There is a website (online store) with many regional versions, each regional version is presented on its subdomain (for example, moscow.mysite.ru, spb.mysite.ru, rostov.mysite.ru, etc.), for the site you need to issue Different robots.txt for each subdomain. I tried to do this:

RewriteCond %{HTTP_HOST} spb.mysite.ru [NC] RewriteRule ^robots\.txt /robots_spb.txt [NC,L] 

did not work. Then in a similar way, but through php

  RewriteCond %{HTTP_HOST} spb.mysite.ru [NC] RewriteRule ^robots\.txt robots.php [L] 

And again nothing. The preferred option would be via php, but I do not know how to implement it. Maybe someone came across? Tell me how to do it.

Thank you in advance!

    3 answers 3

    In general, in .htaccess prescribed

     RewriteRule ^(robots\.txt)$ robots.php [L] 

    And in the robots.php file there is such code:

     $region = strstr(str_ireplace("www.", "", $_SERVER["SERVER_NAME"]), ".", true); if(file_exists("robots_".$region.".txt")) echo file_get_contents("robots_".$region.".txt"); else echo file_get_contents("robots.txt"); 

    Those. counted the region from the URL, checked if there were separate robots for the region, if there was - they substituted it, if not - they substituted the common one for the whole robots.txt site.

       if (file_exists('robots.txt')) { $text = "текст роботса"; $region = тут константой получаем регион, либо ниже $xnumer = explode(".", $_SERVER['SERVER_NAME']); if (strlen($xnumer[0]) == 3) // проверка на www {$region = $xnumer[1];}else{$region = $xnumer[0];} $text = str_replace("{MY_REGION}", $region, $text); $fp = fopen("robots.txt", "w"); fwrite($fp, $text); fclose($fp); } 

      As an option, you can make a check in index.php)) It’s like checking in .htaccess or php), there won't be a particularly strong difference)

      • Thanks, I will try! ) - maler1988

      Not tested, but something like this should happen. Remember part of the subdomain and substitute it to the actual file name.

       RewriteCond %{HTTP_HOST} ^([^.]+).mysite.ru [NC] RewriteRule ^robots\.txt /robots_%1.txt [NC,L] 

      See the RewriteCond backreferences in the RewriteCond documentation.