{"id":3364,"date":"2017-01-24T12:08:45","date_gmt":"2017-01-24T11:08:45","guid":{"rendered":"https:\/\/en.florianbrinkmann.de\/?p=3364"},"modified":"2021-01-21T12:32:31","modified_gmt":"2021-01-21T11:32:31","slug":"modifying-robots-txt-for-individual-sites-of-a-multisite-install","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/modifying-robots-txt-for-individual-sites-of-a-multisite-install-3364\/","title":{"rendered":"Modifying robots.txt for individual sites of a multisite install"},"content":{"rendered":"\n

WordPress creates a robots.txt<\/code> dynamically. To overwrite it in a normal non-multisite installation, you can just upload a static robots.txt<\/code> to the server. On a multisite install, this would overwrite the robots.txt<\/code> for all sites, which is not always the wanted behavior. This post explains how you can modify robots.txt<\/code> for individual sites of a multisite.<\/p>\n\n\n\n\n\n\n\n

WordPress comes with the filter robots_txt<\/code> which allows modifying the dynamically created robots.txt<\/code>\u2019s output. The function get_current_blog_id()<\/code> returns the ID of the current multisite site, which we can use to check for a particular site to add rules to the robots.txt<\/code>. This is how it looks currently for my site:<\/p>\n\n\n

\/**\n * Modify robots.txt for main site and english site\n *\n * @param<\/span> $output\n * @param<\/span> $public\n *\n * @return<\/span> string\n *\/<\/span>\nfunction<\/span> fbn_custom_robots<\/span>( $output, $public )<\/span> <\/span>{\n\t$site_id = get_current_blog_id();\n\tif<\/span> ( $site_id == 1<\/span> ) {\n\t\t$output .= \"Disallow: \/agb-und-widerruf\/\\n\"<\/span>;\n\t\t$output .= \"Disallow: \/mein-konto\/\\n\"<\/span>;\n\t\t$output .= \"Disallow: \/warenkorb\/\\n\"<\/span>;\n\t\t$output .= \"Disallow: \/impressum-und-datenschutz\/\\n\"<\/span>;\n\t} elseif<\/span> ( $site_id == 11<\/span> ) {\n\t\t$output .= \"Disallow: \/account\/\\n\"<\/span>;\n\t\t$output .= \"Disallow: \/cart\/\\n\"<\/span>;\n\t\t$output .= \"Disallow: \/imprint\/\\n\"<\/span>;\n\t\t$output .= \"Disallow: \/terms\/\\n\"<\/span>;\n\t}\n\n\treturn<\/span> $output;\n}\n\nadd_filter( 'robots_txt'<\/span>, 'fbn_custom_robots'<\/span>, 20<\/span>, 2<\/span> );<\/code><\/div>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

For the site with the ID 1<\/code> (this is florianbrinkmann.com) are added four Disallow rules, likewise for the site with the ID 11<\/code> (my English site florianbrinkmann.com\/en).<\/p>\n\n\n\n

To get the site\u2019s ID, I just added the following line after $site_id = get_current_blog_id();<\/code>:<\/p>\n\n\n

$output .= $site_id;<\/code><\/div>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

This way, the ID of the current site is displayed, when you visit its robotx.txt<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"

WordPress creates a robots.txt dynamically. To overwrite it in a normal non-multisite installation, you can just upload a static robots.txt to the server. On a multisite install, this would overwrite the robots.txt for all sites, which is not always the wanted behavior. This post explains how you can modify robots.txt for individual sites of a […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","lazy_load_responsive_images_disabled":false},"categories":[115],"tags":[],"wp-worthy-pixel":{"ignored":false,"public":null,"server":null,"url":null},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3364"}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=3364"}],"version-history":[{"count":4,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3364\/revisions"}],"predecessor-version":[{"id":6068,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3364\/revisions\/6068"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=3364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=3364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=3364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}