0

I'm trying to use this regex to get a different sitemap.xml per host, without the subdomain:

 rewrite "([a-zA-Z\d-]+).([a-zA-Z\d-]+\.([a-z]{2,3}))\/sitemap.xml" /sitemaps/$2.xml; 

As you can see here https://regex101.com/r/WVQQ27/1 the regex works and the 2nd capture group should be the host without subdomain.

But for some reason I keep getting a 404. Any ideas?

The technical background is that we have different sitemaps for different domains but shared content between them as well depending on the language(s) of the content served by the same application.

4
  • What are the log files saying? Also, do you really have the file /sitemaps/world-architects.com.xml present? Commented Jul 5, 2017 at 8:58
  • Yes, it exists, but I figured it out meanwhile: if ($host ~* "([a-zA-Z\d-]+).([a-zA-Z\d-]+\.([a-z]{2,3}))") { set $host_without_www $2; rewrite /sitemap.xml /sitemaps/$host_without_www.xml; } But why does it has to be ~* in the if condition and not ==? However, if is considered evil as well... nginx.com/resources/wiki/start/topics/depth/ifisevil Commented Jul 5, 2017 at 9:04
  • Please write this out as an answer. Commented Jul 5, 2017 at 9:04
  • Please show your complete nginx configuration. There are many settings there that affect the behaviour of your rewrite statement. Commented Jul 5, 2017 at 9:19

2 Answers 2

0
 if ($host ~* "([a-zA-Z\d-]+).([a-zA-Z\d-]+\.([a-z]{2,3}))") { set $host_without_www $2; rewrite /sitemap.xml /sitemaps/$host_without_www.xml; } 

Works but be aware that if is considered evil: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

However, it works for us in this case but if somebody knows a better way to do it I'm open for improving it.

0
map $host $domain { ~(?<cap>[^.]+\.[^.]+)$ $cap; } server { location = /sitemap.xml { try_files /sitemaps/$domain.xml =404; } } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.