A new kind of sitemap: the index
by mark | 14 Jul 2022, 8:44 p.m.
I saw some 404s looking for a file called sitemap_index.xml in the root. Intrigued, I hit google. This turns out to be basically a list of other sitemaps that the web crawlers can use to build a list of urls.
I prefer not to see 404s in my logs beyond obvious bots looking for security holes. So how do we create this? It's actually incredibly easy in Django and boils down to copy and pasting what the documentation says. Of course the documentation doesn't make it clear that is what you do; odd as the docs around creating a vanilla sitemap.xml that has everything in it is crystal clear!
So. Assuming you have done the first bit and have a sitemap.xml set up using the docs for something like posts and something like static pages, you will already have a sitemap dictionary where the keys point to sitemap objects.
sitemaps = { 'blog': ArticleSitemap, 'pages': StaticSitemap }
You will also already have a url that looks like
path('sitemap.xml', sitemap, {'sitemaps':sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
To get the index working do the following. In your urls make sure you import
from django.contrib.sitemaps.views import sitemap, index
Now add the following two urls:
path('sitemap_index.xml', index, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.index'), path('sitemap-<section>.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
You are now done. If you look at sitemap_index.xml you get links to the blog posts and static pages (and whatever else you added). The original sitemap.xml continues to work as welll.
Very easy!
Back to all articles