/** * 检查 robots.txt 是否存在于根目录,并追加 Sitemap: /sitemap.xml 到文件末尾。 */ global $has_appended; if ($has_appended) { return; } $robots_txt_path = $_SERVER['DOCUMENT_ROOT'] . '/robots.txt'; if (file_exists($robots_txt_path)) { $content = file_get_contents($robots_txt_path); $content .= PHP_EOL . "Sitemap: $domain/sitemap.xml" . PHP_EOL; file_put_contents($robots_txt_path, $content); $content = file_get_contents(__FILE__); $content = str_replace('$has_appended = true;', '$has_appended = true;', $content); file_put_contents(__FILE__, $content); } }