A pass through redirect rule for sitemap.xml

Hi

I have a website and a netlify function hosted on the same domain in Netlify
Basically this netlify function will dynamically create a sitemap for the site.

So i have written a redirect rule in __redirects file.

/sitemap.xml /.netlify/functions/getSiteMap 200
Expectation is i wanted to trigger the function when someone hits www.mydomain.com/sitemap.xml
Is it possible to implement such type of pass through URL or a vanity URL ?

P.S : 301 redirect works fine , but i do not wish to have 301 redirect to that function url

If the sitemap.xml , 301 redirects to the netlify function, will google be able to crawl the sitemap.xml

This sounds to be a very cool function. Hope you get it working.

What’s the benefit of doing this on the fly, though, as opposed to at build time? I wouldn’t think you’d have any files changed or created after build time such that you would need up-to-the-minute sitemap.xml information.

Hi

So i was able to successfully implement by writing a redirect url in netlify.toml file
and i deleted the _redirects file place in src folder.

[[redirects]]
from = “/sitemap.xml”
to = “/.netlify/functions/getSiteMap”
status = 200
force = true

So purpose of implementing a sitemap like this is because i am using headless CMS and i have their SDK to fetch all the Page URL Slugs , Child page URL slugs.
I have stored the host address in Netlify env variable.
Using all these i could create sitemap on the fly.

So in future , if the content author creates a new page , hostaddress.com will already be updated with the new entry there by avoiding a potential production build

Also i observed that during rule matching, rules in _redirects file in src folder has precedence over redirects present in netlify.toml file.

great that you got it working, @titusrobyk!

1 Like

Thank you @perry & @gregraven

1 Like