0

I got redirections working by writing this config file:

map $request_uri $new_uri {
    default https://github.com;
    /www http://wow.com;
    /?p=1 http://wow.com/p1;
}
server {
    listen 80;
    server_name TestDomain.com www.TestDomain.com;

    location / {
        if ($new_uri) {
            return 301 $new_uri;
        }
    }
}

Now, I would also like to control whether its a 301 or 302 with each redirection, so I tried doing this:

map $request_uri $new_uri {
    default https://github.com;
    /www http://wow.com;
    /?p=1 http://wow.com/p1;
}
map $request_uri $ret_code {
    default 302;
    /www 302;
    /?p=1 302;
}
server {
    listen 80;
    server_name TestDomain.com www.TestDomain.com;

    location / {
        if ($new_uri) {
            return $ret_code $new_uri;
        }
    }
}

But I can't seem to get the return statement right, it complains of invalid return code when I test the config file. How do I go about specifying the last line if I have both the return code and the url in variables?

Also, any pointers on how can I configure default to be:

map $request_uri $new_uri {
    default https://github.com$request_uri;

so that default redirection rule is set as testdomain.com/whatever to github.com/whatever?

6
  • The third redirect won't work, because nginx $request_uri does not contain query arguments. Jan 15, 2017 at 9:39
  • @TeroKilkanen Its actually working. See nginx.org/en/docs/http/…
    – Ashfame
    Jan 15, 2017 at 9:41
  • Do you really see the Location: http://wow.com/p1 in the HTTP headers when you request /?p=1? Jan 15, 2017 at 9:46
  • Yep! $ curl -I testdomain.com/?p=1 HTTP/1.1 301 Moved Permanently Server: nginx/1.10.0 (Ubuntu) Date: Sun, 15 Jan 2017 09:42:13 GMT Content-Type: text/html Content-Length: 194 Connection: keep-alive Location: http://wow.com/p1 ret-code: 302
    – Ashfame
    Jan 15, 2017 at 9:46
  • My mistake, it was a different case where query arguments are not visible. Jan 15, 2017 at 9:49

2 Answers 2

0

I don't know how to implement the status code.

For the default option, you can try this:

map $request_uri $new_uri {
    default DEFAULT;
    ...
}

server {
    listen 80;
    server_name TestDomain.com www.TestDomain.com;

    location / {
        if ($new_uri = "DEFAULT") {
            return $ret_code https://github.com$request_uri;
        }
        if ($new_uri) {
            return $ret_code $new_uri;
        }
    }
}
0
0

Solved by using multiple ifs:

map $request_uri $new_uri {
    default DEFAULT;
    /www http://wow.com;
    /?p=1 http://wow.com/p1;
}
map $request_uri $ret_code {
    default 507;
    /www 302;
    /?p=1 301;
}
server {
    listen 80;
    server_name TestDomain.com www.TestDomain.com;

    location / {
        if ($new_uri = 'DEFAULT') {
                return 301 https://github.com$request_uri;
        }

        if ($ret_code = 301) {
            return 301 $new_uri;
        }

        if ($ret_code = 302) {
                return 302 $new_uri;
        }
    }
}

I will leave the question unaccepted for a while to see if there any any pitfalls with it and there is a better way to do it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .