-1

I have multiple domains example-A.com,example-B.com andexample-C.com that are all different, but they all point to the same folder on my server (The three domains are very closely connected but only administrated through a single Laravel application, for more see Multiple websites with one Laravel installation)

Now I need for each domain a different sitemap.xml file.

I found in this answer how to execute PHP inside XML files. With that I could dynamically change the content of the sitemap.xml with respect to the url. Will this work for crawlers and SE if they try to access the XML file? Or is there a more recommended way of doing this?

Adam
  • 25,960
  • 22
  • 158
  • 247
  • _“Will this work for crawlers and SE if they try to access the XML file?”_ - first of all, they do not access a _file_, but a _resource_, and secondly, why would it not? What your server does internally to serve the content behind _any_ URL is absolutely irrelevant to the outside world. – misorude Oct 26 '18 at 07:14
  • @misorude why is `sitemap.xml` not a file? Yes this is what I thought, but I just wasn't sure. Sorry if my question offended you. – Adam Oct 26 '18 at 07:37
  • 1
    It is a file when you look at it via your local file system. Once you start accessing it via HTTP(S) - which is what crawlers will do, since they don’t have access to the file system of your server - it stops being a file, because HTTP does not _have_ the concept of a “file”. So what the data is read from or how it is dynamically created by your web server, does not matter one bit for anyone on the outside. – misorude Oct 26 '18 at 07:40

1 Answers1

1

Have a route that's matched by sitemap.xml but leads to a PHP script. Then this script serves a pre-generated sitemap file, or maybe even generates it itself. The simplest way I can imagine would be:

# .htaccess
RewriteRule ^/sitemap.xml$ sitemap.php [L]

// sitemap.php
switch ($_SERVER['HTTP_HOST']) {
    case 'example-A.com':
        echo file_get_contents(__DIR__ . '/sitemap-A.xml');
        break;
    case 'example-B.com':
        echo file_get_contents(__DIR__ . '/sitemap-B.xml');
        break;
    // etc.
}

I don't know Laravel, but I'm sure there's a way to create a route that's matched by /sitemap.xml and served by some controller.

Bartosz Zasada
  • 3,762
  • 2
  • 19
  • 25
  • 1
    Thanks for this idea! Maybe its best to use the RewriteRule based on the `{HTTP_HOST}` and then link to `sitemap-A.xml` or `sitemap-B.xml`. Then one does not need a `sitemap.php` and the switch rule. – Adam Oct 26 '18 at 07:25