Nginx config to redirect to $URI

Solution 1:

Use server_name .example.com; instead.

From nginx's server names documentation :

A special wildcard name in the form “.example.org” can be used to match both the exact name “example.org” and the wildcard name “*.example.org”.

Note that www.example.com can be removed as the wildcard form will match it anyway.