3

I've recently swapped the domain extension of a site from .xyz to .com and I'm using a rule in my nginx config to 301 traffic from the old domain to the new one.

server {
    listen 8080;
    server_name example.xyz www.example.xyz;
    return 301 https://www.example.com$request_uri;
  }

However I want to continue serving a sitemap at example.xyz/sitemap.xml

Is there a rule I can implement which will take preference over the 301 just for a single location? The sitemap can be a static file so I can use an alias but not sure how to stop the 301 taken effect for just that url?

user1803975
  • 345
  • 1
  • 4
  • 14

1 Answers1

2

Move the return statement into a location / block. Then you can add a location = block to match a single URI. For example:

server {
    listen 8080;
    server_name example.xyz www.example.xyz;

    location / {
        return 301 https://www.example.com$request_uri;
    }
    location = /sitemap.xml {
        root /path/to/enclosing/directory;
    }
}

See this document for details.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Thanks Richard! Would this configuration apply the 301 to all urls except "/sitemap.xml"? So for example would "example.xyz/some-random-page" still get redirected to "example.com/some-random-page" – user1803975 May 06 '18 at 18:22
  • @user1803975 Yes - all URIs except the one matched by the `location =` block. – Richard Smith May 06 '18 at 18:27