When you move content, change domains or create aliases on the web, you often use a redirect, like a URL rewrite in your webserver config, to tell browsers that the link should point to something else — but what if you want to move an API that handles POST requests?
URL rewrites are useful, because you can tell the browser to change only a specific part of the original link, like the domain, but keep the rest intact. It’s particularly useful when you move your blog to a different address, but want to keep all the permalinks working.
However, if you are migrating an API things get tricky. Normally, rewrites work on the URL level and drop the request body, which contains POST values. With URL rewrites, your migrated API looks like working but in fact can’t receive any POST payload — redirect POST requests won’t work with your usual 301/302 URL rewrite redirects!
Luckily there’s a simple solution to this problem. Many people suggest configuring a POST-forwarding proxy on your webserver, but that’s not optimal. Your best option is to use a 307 Redirect.
In Apache, the syntax is as follows:
Redirect 307 /old(.*) https://new.com$1
While in Nginx it’s as follows:
location ~* /old { return 307 https://new.com$request_uri; }
Note: HTTP Code 307 is a temporary redirect. The 308 is permanent, however it’s a newer code and some old browsers or libs may not support it.