Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global sitemap, linking to each multi-site site #19

Open
SteveEdson opened this issue Mar 20, 2024 · 3 comments
Open

Global sitemap, linking to each multi-site site #19

SteveEdson opened this issue Mar 20, 2024 · 3 comments

Comments

@SteveEdson
Copy link

Bug description

Currently, each site has a sitemap, e.g /sitemap.xml, /fr/sitemap.xml etc.

It would be great if it could generate a global file, such as /sitemap-index.xml at the main root, which links to each sites main sitemap. A sitemap of sitemaps if you like.

Steps to reproduce

See above

Environment and versions

Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 2
Antlers: runtime
Sites: 25 (UK / English, Belgium / English, Belgium / French, and 22 more)
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.49.0 PRO

Statamic Addons
mitydigital/sitemapamic: 2.3.9
statamic/ssg: 2.2.0


### Additional details

_No response_
@martyf
Copy link
Contributor

martyf commented Mar 21, 2024

In your example, what is your sitemap.xml? Your default site?

I get what you're saying but also see cases where you wouldn't want it, so feel it should be a config option.

What makes most sense for this though? For sitemap.xml to be the index, and the default site (if root) to be renamed perhaps? Less confusion as there's a single sitemap only. Gets a bit more awkward if you're using different domains (or subdomains) for each site as you're then only having a sitemap.xml per site, without a language prefix.

Trying to think of the cleanest way to tackle this. Love your input and thoughts to flesh out what is the simplest approach for the dev without making it too confusing as to which is which.

I'm just in the middle of moving interstate so apologies for the delay.

@SteveEdson
Copy link
Author

Google has some recommendations for this:

"The XML format of a sitemap index file is very similar to the XML format of a sitemap file, and it's defined by the Sitemap Protocol. This means that all the sitemap requirements apply to sitemap index files also.

The referenced sitemaps must be hosted on the same site as your sitemap index file."

Sitemaps that are referenced in the sitemap index file must be in the same directory as the sitemap index file, or lower in the site hierarchy. For example, if the sitemap index file is at https://example.com/public/sitemap_index.xml, it can only contain sitemaps that are in the same or deeper directory, like https://example.com/public/shared/....
https://developers.google.com/search/docs/crawling-indexing/sitemaps/large-sitemaps#:~:text=The%20XML%20format%20of%20a,as%20your%20sitemap%20index%20file.

So in my mind, the sitemap index would only list the sitemaps which exist on the same domain on subpaths, and the sitemap.xml would be the same as it is now

@SteveEdson
Copy link
Author

SteveEdson commented Mar 25, 2024

For the time being, I've created this myself (without any config options etc), it's simply this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\ArrayToXml\ArrayToXml;

class SitemapIndexController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request)
    {
        $sitemap = [
            '@attributes' => [
                'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
            ],
            'sitemap' => [],
        ];

        $sites = config('statamic.sites');        
        foreach ($sites['sites'] as $site) {
            $sitemap['sitemap'][] = [
                'loc' => url($site['url'] . 'sitemap.xml'),
            ];
        }

        $sitemap = ArrayToXml::convert($sitemap, 'sitemapindex', true, 'UTF-8');


        return response($sitemap)
            ->header('Content-Type', 'text/xml');
    }
}

Which produces:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://www.example.com/sitemap.xml</loc>
  </sitemap>
  <sitemap>
    <loc>https://www.example.com/fr/sitemap.xml</loc>
  </sitemap>
</sitemapindex>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants