Search code examples
http-redirectnginxurisubdomainresponse

How redirect dynamic subdomain to a domain with uri


I have this scenario. I want to redirect in nginx all subdomains in this format.

(profile_name).mydomain.com mydomain.com/profile/(profile_name)

How can I do this in nginx?

Thanks.


Solution

  • You could use a regular expression server block.

    server {
        server_name ~^(?<name>.+)\.example\.com$;
        return 301 http://example.com/profile/$name$request_uri;
    }
    

    See this document for details.