I have multiples subdomains that I want to redirect in nginx or php like that

```
Example: test.test2.domain.com to domain.com/test/test2

```

It's possible to do a 301 Redirect in nginx so in php I can get the parts test and test2 as params ?

Thanks