I'm trying to rewrite my MediaWiki sitemap URLs to their proper location on S3 via Nginx, given that MediaWiki generates the sitemap.xml page with URLs like:
https://subdomain.domain.net/databasename/sitemaps/sitemap-databasename-NS_0-0.xml.gz; they are in the same format in S3, only the domain is 'static.domain.net' instead of the subdomain of the actual site.
I've tried several different rewrite rules, but this one has gotten me the closest to what I need:
location ~ /([^/]+)/sitemaps/(sitemap-[^\/]+\.xml\.gz)$ {
rewrite https://static.domain.net/$1/sitemaps/$2 permanent;
}
But that rewrites the url to be:
https://static.domain.net/databasename/sitemaps/, without the second capture group. I've tested the regex in regex101, and it does seem to match both groups, but for some reason, Nginx is missing the second capture group when it redirects — what is causing this? I simply need to capture the two groups and redirect the URL to static.domain.net instead of subdomain.domain.net.
Use either
or
Don't mix up the syntax -
rewritetakes a regular expression as the first argument, and does not simply re-use capture groups from the outer context.