server { root foo; location ~ /sites/(?.+)/(?.+) { try_files $uri @redirect; } location @redirect { rewrite ^ http://$domain/$uri_part permanent; } }