Search code examples
httpnginxhttpserver

Redirect subdomain to main domain except specific URLs - Nginx


I have a main domain as follows and am using Nginix server

Main domain : https://website.com/

Sub domain : https://admin.website.com/

If anyone accesses https://admin.website.com/ It needs to redirect to https://website.com/ - I have found the following code which uses a combination of multiple servers (including the one with wildcard subdomain):

server {
    listen          80;
    server_name     admin.website.com;
    add_header      Content-Type    text/plain;
    return  200     "admin";
}
server {
    listen          80;
    server_name     *.website.com;
    return  301     $scheme://example.com$request_uri;
}
server {
    listen          80;
    server_name     website.com;
    add_header      Content-Type    text/plain;
    return  200     "main";
}

But from the subdomain, if anyone accesses the following url https://admin.website.com/login and any subsequent pages, for example:

etc...

Then it should not redirect to the main domain. It should stay on that sub domain page. Does anyone have any ideas?


Solution

  • What about

    server {
        listen          80;
        server_name     admin.website.com;
        ...
        location / {
            return 301 $scheme://website.com$request_uri;
        }
        location /login {
            # processing URL here
            root </path/to/root>;
            ...
        }
    }