Brian
Brian

Reputation: 13571

nginx redirect POST requests

I have a web server which accepts https://www.example.com/API/ooo/xxx, https://www.example.com/API/aaa/bbb, and https://www.example.com/API/xxx/yyy ... etc.

Now we want to redirect the above quests to https://www.example.com/ooo/xxx, https://www.example.com/aaa/bbb, and https://www.example.com/xxx/yyy ... etc.

I've tried using rewrite key word in nginx:

location /API/ {
    rewrite ^/API(.*)$ https://$host$1 redirect;
}

This works for GET requests. But it turns POST requests to GET requests. This is something I don't want.

How can I preserve the http method while redirecting /API/* to /*?

This post says that I can use 307 redirect. But rewrite doesn't seem to support 307 redirect. And I can't figure out how to use $1 regular expression property in return.

Upvotes: 2

Views: 7508

Answers (1)

Richard Smith
Richard Smith

Reputation: 49682

Use a return statement. You can use a regular expression location block to capture the part of the URI to return.

For example:

location ~ ^/API(/.*)$ {
    return 307 $1$is_args$args;
}

Note that with the regular expression location directive, its order within the configuration is significant, so you may need to move it. See this document for details.

Upvotes: 3

Related Questions