Replace domain for sitemap.xml

Hi.
I have a lots of different domains and I need to get sitemap.xml from a api using the domain in the request.
I can redirect the traffic using this middleware, but I need to replace it so it still shows like https://anotherexample.com/sitemap.xml for the request and not https://api.example.com/api/sitemap?host=anotherexample.com in the browser.

http:
  middlewares:
    sitemap:
      redirectRegex:
        regex: ^https://?(.+)/sitemap.xml
        replacement: https://api.example.com/api/sitemap?host=${1}

A redirect tells the browser to go to a different URL, so that probably won’t work.

Instead you can create a router and service for the domain and path. Use loadbalancer.servers.url to set the external target URL.

Note this only works in a dynamic config file, not on labels.

Thanks, I needed a service together with the middleware to get it to work like I needed.
For those wondering this is my new yaml

http:
  routers:
    sitemap-test:
      entrypoints:
        - web-secure 
      service: service-sitemaptest
      rule: "Path(`/sitemap.xml`)" 
      priority: 198
      tls: true
  services:
    service-sitemaptest:
      loadbalancer:
        servers:
        - url: "https://api.example.com" 
        passHostHeader: true
  middlewares:
    sitemap:
      replacePathRegex:
        regex: ^https://?(.+)/sitemap.xml  
        replacement: /api/sitemap?host=${1} 
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.