Monday , February 3 2025

Single robots for multidomains on WordPress without plugins

This article is a continuation of multisite cases on WordPress. Here is an example of replacing the standard robots.txt with a multisite one.

1. If robots.txt file has already been created, rename it or delete it.
2. In .htaccess add the line:

RewriteRule ^robots.txt$ robots.php

3. Create a robots.php file with the following content:

<?php
  header('Content-Type: text/plain; charset=utf-8');

  echo 'User-agent: *
        Disallow: /wp-admin/
        Allow: /wp-admin/admin-ajax.php
        ';

  if($_SERVER['HTTP_HOST']=='subdomain1.domain.com'){
    echo 'Host: https://subdomain1.domain.com
        Sitemap: https://subdomain1.domain.com/sitemap_index.xml';
  } 
  else if($_SERVER['HTTP_HOST']=='subdomain2.domain.com'){
    echo 'Host: https://subdomain2.domain.com
        Sitemap: https://subdomain2.domain.com/sitemap_index.xml';
  }

   else {
    echo 'Host: https://domain.com
Sitemap: https://domain.com/sitemap_index.xml';
  }
?>

Insert all the main content into the first echo, the rest of the dynamic will be placed according to the conditions with the host check.

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

How to remove 404 //fonts.gstatic.com and //fonts.googleapis.com from WordPress head section

I tried solutions with functions that should remove from head and , but a very …

Leave a Reply

Your email address will not be published. Required fields are marked *