{"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<p class=\"wp-block-paragraph\">WordPress creates a <code class=\"lang-markup\">robots.txt<\/code> dynamically. To overwrite it in a normal non-multisite installation, you can just upload a static <code class=\"lang-markup\">robots.txt<\/code> to the server. On a multisite install, this would overwrite the <code class=\"lang-markup\">robots.txt<\/code> for all sites, which is not always the wanted behavior. This post explains how you can modify <code class=\"lang-markup\">robots.txt<\/code> for individual sites of a multisite.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">WordPress comes with the filter <code class=\"lang-php\">robots_txt<\/code> which allows modifying the dynamically created <code class=\"lang-markup\">robots.txt<\/code>\u2019s output. The function <code class=\"lang-php\">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 <code class=\"lang-markup\">robots.txt<\/code>. This is how it looks currently for my site:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/**\n * Modify robots.txt for main site and english site\n *\n * <span class=\"hljs-doctag\">@param<\/span> $output\n * <span class=\"hljs-doctag\">@param<\/span> $public\n *\n * <span class=\"hljs-doctag\">@return<\/span> string\n *\/<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">fbn_custom_robots<\/span><span class=\"hljs-params\">( $output, $public )<\/span> <\/span>{\n\t$site_id = get_current_blog_id();\n\t<span class=\"hljs-keyword\">if<\/span> ( $site_id == <span class=\"hljs-number\">1<\/span> ) {\n\t\t$output .= <span class=\"hljs-string\">\"Disallow: \/agb-und-widerruf\/\\n\"<\/span>;\n\t\t$output .= <span class=\"hljs-string\">\"Disallow: \/mein-konto\/\\n\"<\/span>;\n\t\t$output .= <span class=\"hljs-string\">\"Disallow: \/warenkorb\/\\n\"<\/span>;\n\t\t$output .= <span class=\"hljs-string\">\"Disallow: \/impressum-und-datenschutz\/\\n\"<\/span>;\n\t} <span class=\"hljs-keyword\">elseif<\/span> ( $site_id == <span class=\"hljs-number\">11<\/span> ) {\n\t\t$output .= <span class=\"hljs-string\">\"Disallow: \/account\/\\n\"<\/span>;\n\t\t$output .= <span class=\"hljs-string\">\"Disallow: \/cart\/\\n\"<\/span>;\n\t\t$output .= <span class=\"hljs-string\">\"Disallow: \/imprint\/\\n\"<\/span>;\n\t\t$output .= <span class=\"hljs-string\">\"Disallow: \/terms\/\\n\"<\/span>;\n\t}\n\n\t<span class=\"hljs-keyword\">return<\/span> $output;\n}\n\nadd_filter( <span class=\"hljs-string\">'robots_txt'<\/span>, <span class=\"hljs-string\">'fbn_custom_robots'<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">2<\/span> );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">For the site with the ID <code class=\"lang-php\">1<\/code> (this is florianbrinkmann.com) are added four Disallow rules, likewise for the site with the ID <code class=\"lang-php\">11<\/code> (my English site florianbrinkmann.com\/en).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get the site\u2019s ID, I just added the following line after <code class=\"lang-php\">$site_id = get_current_blog_id();<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$output .= $site_id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This way, the ID of the current site is displayed, when you visit its <code class=\"lang-markup\">robotx.txt<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-3364","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"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","targetHints":{"allow":["GET"]}}],"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}]}}